การเชื่อมโยงข้อมูลผู้ใช้กับการยืนยันตัวตน
เชื่อมโยงรหัสผู้ใช้ที่ผ่านการยืนยันตัวตนกับรายการข้อมูลที่สอดคล้องกันในฐานข้อมูลแบบเรียลไทม์ เพื่อสร้างเนื้อหาเฉพาะบุคคล
การเชื่อมโยงข้อมูลผู้ใช้กับการยืนยันตัวตน เป็นบทเรียน Firebase Auth & Realtime Database Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Firebase Auth & Realtime Database Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Link Auth & Database
Why connect user authentication with your database? It's crucial for building personalized and secure applications.
By linking, you can:
- Store user-specific data like profiles or settings.
- Control access to data based on who is logged in.
- Create personalized experiences for each user.
Accessing the User's UID
Every authenticated user in Firebase has a unique User ID (UID). This ID is your key to linking their authentication status to their data.
You can get the current user's UID from the currentUser object after a user signs in.
<!-- Assume Firebase SDKs are loaded -->
<!-- and firebase.initializeApp() has been called -->
<script>
// Listen for authentication state changes
firebase.auth().onAuthStateChanged(user => {
if (user) {
const uid = user.uid;
console.log("Current User UID:", uid);
// Example: Display UID in a web element
// document.getElementById('user-uid').innerText = uid;
} else {
console.log("No user signed in.");
}
});
</script>UID as Your Database Key
Once you have the UID, you can use it as a unique identifier in your Firebase Realtime Database paths. This creates a secure and organized way to store user-specific data.
Think of it like a folder for each user, named after their UID:
/users/{UID}/profile/users/{UID}/preferences
Storing First User Data
Let's store some basic profile information for a signed-in user. We'll use the user's UID to create a unique path in the database.
This example stores a username and email under /users/{uid}.
<!-- Assume Firebase SDKs loaded & initialized -->
<script>
const user = firebase.auth().currentUser;
if (user) {
const uid = user.uid;
const userRef = firebase.database().ref('users/' + uid);
userRef.set({
username: "JaneDoe",
email: user.email, // Use auth email
createdAt: firebase.database.ServerValue.TIMESTAMP
})
.then(() => {
console.log("User data saved successfully!");
})
.catch(error => {
console.error("Error saving user data:", error);
});
} else {
console.log("No user signed in to save data for.");
}
</script>Your User Data Structure
After the previous code runs, your Realtime Database might look something like this:
{
"users": {
"YOUR_USER_UID_HERE": {
"username": "JaneDoe",
"email": "janedoe@example.com",
"createdAt": 1678886400000
}
}
}Each user gets their own top-level key under /users, making it easy to manage their data.
Reading User-Specific Data
To retrieve personalized content, you simply read from the path constructed with the current user's UID. This ensures they only see their own information.
Here's how to fetch the profile data we just stored:
<!-- Assume Firebase SDKs loaded & initialized -->
<script>
const user = firebase.auth().currentUser;
if (user) {
const uid = user.uid;
const userRef = firebase.database().ref('users/' + uid);
userRef.on('value', (snapshot) => {
const userData = snapshot.val();
if (userData) {
console.log("User Data:", userData);
console.log("Username:", userData.username);
} else {
console.log("No user data found for this UID.");
}
}, (error) => {
console.error("Error reading user data:", error);
});
} else {
console.log("No user signed in to read data for.");
}
</script>Updating User Profile
Users might want to change their profile information. You can easily update specific fields within their UID's data path using the update() method.
This updates only the username field without affecting others.
<!-- Assume Firebase SDKs loaded & initialized -->
<script>
const user = firebase.auth().currentUser;
if (user) {
const uid = user.uid;
const userRef = firebase.database().ref('users/' + uid);
userRef.update({
username: "Jane_The_Explorer" // Update only username
})
.then(() => {
console.log("Username updated successfully!");
})
.catch(error => {
console.error("Error updating username:", error);
});
} else {
console.log("No user signed in to update data for.");
}
</script>Handling Missing Data
It's important to always check if a user is signed in (firebase.auth().currentUser is not null) before trying to access their UID or database path.
Also, when reading data, check if snapshot.val() returns actual data or null. This prevents errors if a user's data hasn't been created yet.
Linking Best Practices
For efficiency and scalability, consider these tips:
- Keep UIDs consistent: Always use the authenticated UID as the primary key for user data.
- Denormalize for lookups: If you often need a user's name next to another piece of data (e.g., a post), store a copy of the name with the post instead of always querying the user profile.
- Security Rules: Use Realtime Database Security Rules to enforce that only the authenticated user can read/write their own data. (More on this in a later lesson!)
Quick Check: User Data
You want to store a user's favorite color in the Realtime Database, ensuring it's linked to their authenticated profile. Which path correctly uses the user's UID as a key?
Recap: Connecting User Data
In this lesson, you learned how to effectively link authenticated users to their specific data in the Firebase Realtime Database.
- We accessed the unique User ID (UID).
- We used the UID as a key in the database path.
- We practiced storing and reading user-specific information.
- We covered important considerations for handling missing data.
This foundational skill is vital for building personalized and secure applications!
คำถามที่พบบ่อย
บทเรียน “การเชื่อมโยงข้อมูลผู้ใช้กับการยืนยันตัวตน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเชื่อมโยงข้อมูลผู้ใช้กับการยืนยันตัวตน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Firebase Auth & Realtime Database Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเชื่อมโยงข้อมูลผู้ใช้กับการยืนยันตัวตน”
เชื่อมโยงรหัสผู้ใช้ที่ผ่านการยืนยันตัวตนกับรายการข้อมูลที่สอดคล้องกันในฐานข้อมูลแบบเรียลไทม์ เพื่อสร้างเนื้อหาเฉพาะบุคคล คุณปฏิบัติ Firebase Auth & Realtime Database Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Firebase Auth & Realtime Database Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Firebase Auth & Realtime Database Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การเชื่อมโยงข้อมูลผู้ใช้กับการยืนยันตัวตน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Firebase Auth & Realtime Database Apps นี้ได้ไหม
ได้ บทเรียน Firebase Auth & Realtime Database Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเชื่อมโยงข้อมูลผู้ใช้กับการยืนยันตัวตน
- โปรไฟล์ผู้ใช้แบบเรียลไทม์
- การแก้ไขข้อมูลร่วมกัน
- การควบคุมการเข้าถึงข้อมูลผู้ใช้ตามบทบาท