0Pricing
Firebase Auth & Realtime Database Apps · บทเรียน

การจัดการเซสชันและสถานะผู้ใช้

เรียนรู้วิธีติดตามสถานะการยืนยันตัวตนของผู้ใช้ เก็บรักษาเซสชัน และจัดการโปรไฟล์ผู้ใช้อย่างปลอดภัย

การจัดการเซสชันและสถานะผู้ใช้ เป็นบทเรียน Firebase Auth & Realtime Database Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Firebase Auth & Realtime Database Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

User Sessions & States Intro

Welcome! In this lesson, we'll dive into managing user sessions and understanding authentication states in Firebase. These are crucial for building secure and user-friendly apps.

We'll cover how to:

  • Monitor when a user logs in or out.
  • Keep users logged in across app restarts.
  • Update user profile information.

Tracking User Status

Firebase Authentication provides a powerful way to monitor a user's login status in real-time. This is done using an "authentication state listener."

  • It tells your app if a user is currently logged in or logged out.
  • It fires whenever the user's authentication state changes (e.g., login, logout, token refresh).
  • This is perfect for updating UI elements or redirecting users.

Live Auth State Listener

Here's how you set up an authentication state listener. It's a key part of making your app react to user logins and logouts.

This example assumes Firebase is initialized and the auth service is available.

import { getAuth, onAuthStateChanged } from "firebase/auth";
const auth = getAuth();

// This function will be called whenever the auth state changes
onAuthStateChanged(auth, (user) => {
  if (user) {
    // User is signed in
    console.log("User logged in:", user.email);
    // You can access user.uid, user.displayName, etc.
  } else {
    // User is signed out
    console.log("No user logged in.");
  }
});

console.log("Auth state listener set up.");

Session Persistence Explained

Imagine your user logs in, closes the app, and reopens it only to find they're logged out. Frustrating, right?

Firebase Auth solves this with "session persistence." It allows you to specify how long a user's login session should last, even after they close their browser or app.

  • Firebase stores user credentials securely.
  • It automatically re-authenticates the user when they return.

Choosing Persistence Options

Firebase offers different levels of session persistence to suit various application needs:

  • LOCAL: The user's session is persisted even if the browser window is closed. (Default for web)
  • SESSION: The session is persisted only for the current browser session. It's cleared when the window is closed.
  • NONE: No session persistence. The user is logged out when the page is refreshed or the app restarts.

Customizing Session Persistence

You can set the desired persistence level before a user signs in. This tells Firebase how to handle the session for that specific login.

Here's an example of setting it to SESSION for a temporary login.

import { getAuth, setPersistence, browserSessionPersistence } from "firebase/auth";
const auth = getAuth();

// Set persistence to SESSION
setPersistence(auth, browserSessionPersistence)
  .then(() => {
    console.log("Persistence set to SESSION.");
    // Now you can sign in your user
    // signInWithEmailAndPassword(auth, email, password);
  })
  .catch((error) => {
    console.error("Error setting persistence:", error.message);
  });

console.log("Attempting to set persistence...");

Accessing User Info

Once a user is logged in, you often need to access their information like their ID, email, or display name. Firebase makes this easy.

  • The currentUser property of the auth object holds the currently logged-in user.
  • It will be null if no user is logged in.
  • Important: Always check currentUser inside an onAuthStateChanged listener to ensure it's up-to-date.

Managing User Profiles

Firebase Auth allows users to update basic profile information directly. This includes their display name and profile photo URL.

These updates are client-side and don't require re-authentication for the user to see the changes in their own profile object.

  • Use the updateProfile method on the user object.
  • Common updates: displayName and photoURL.

Changing Display Name

Let's see how to update a user's display name. This is useful for personalizing their experience in your app.

This operation requires a logged-in user.

import { getAuth, updateProfile } from "firebase/auth";
const auth = getAuth();

const user = auth.currentUser; // Get the current user

if (user) {
  updateProfile(user, {
    displayName: "Jane Doe",
    // photoURL: "https://example.com/jane-profile.jpg"
  }).then(() => {
    console.log("Profile updated successfully!");
    console.log("New display name:", user.displayName);
  }).catch((error) => {
    console.error("Error updating profile:", error.message);
  });
} else {
  console.log("No user is logged in to update profile.");
}

console.log("Attempting to update user profile...");

Quick Check: Auth State

Consider a web application using Firebase Authentication. A user logs in, then closes their browser and reopens it a few minutes later. They find themselves still logged in.

Which Firebase Auth persistence setting was most likely active?

Recap: Sessions & States

Great job! In this lesson, you learned how to effectively manage user sessions and states in your Firebase application.

  • We explored onAuthStateChanged for real-time user status monitoring.
  • You understood the importance of session persistence and its different levels (LOCAL, SESSION, NONE).
  • Finally, you learned how to access and update a logged-in user's profile information.

These skills are fundamental for building dynamic and personalized user experiences!

คำถามที่พบบ่อย

บทเรียน “การจัดการเซสชันและสถานะผู้ใช้” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจัดการเซสชันและสถานะผู้ใช้” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การจัดการเซสชันและสถานะผู้ใช้” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Firebase Auth & Realtime Database Apps นี้ได้ไหม

ได้ บทเรียน Firebase Auth & Realtime Database Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การใช้งานการยืนยันตัวตนด้วยอีเมลและรหัสผ่าน
  2. การจัดการเซสชันและสถานะผู้ใช้
  3. การจัดการข้อผิดพลาดในการยืนยันตัวตน
  4. การรีเซ็ตรหัสผ่านและการยืนยันอีเมล
← กลับไปที่ Firebase Auth & Realtime Database Apps