จัดการผู้ใช้ใน Firebase

จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ

สร้างผู้ใช้

คุณสร้างผู้ใช้ใหม่ในโปรเจ็กต์ Firebase โดยเรียกใช้เมธอด createUserWithEmailAndPassword หรือลงชื่อเข้าใช้ผู้ใช้เป็นครั้งแรกโดยใช้ผู้ให้บริการข้อมูลประจำตัวที่ติดต่อกับภายนอก เช่น Google Sign-In หรือ Facebook Login

คุณยังสามารถสร้างผู้ใช้ที่ตรวจสอบสิทธิ์ด้วยรหัสผ่านใหม่ได้จากส่วนการตรวจสอบสิทธิ์ของ คอนโซล Firebase บนหน้าผู้ใช้ หรือโดยใช้ Admin SDK

รับผู้ใช้ที่ลงชื่อเข้าใช้อยู่ในปัจจุบัน

วิธีที่แนะนำในการรับผู้ใช้ปัจจุบันคือการตั้งค่าผู้สังเกตการณ์บนอ็อบเจ็กต์ Auth:

Web version 9

import { getAuth, onAuthStateChanged } from "firebase/auth";

const auth = getAuth();
onAuthStateChanged(auth, (user) => {
  if (user) {
    // User is signed in, see docs for a list of available properties
    // https://firebase.google.com/docs/reference/js/firebase.User
    const uid = user.uid;
    // ...
  } else {
    // User is signed out
    // ...
  }
});

Web version 8

firebase.auth().onAuthStateChanged((user) => {
  if (user) {
    // User is signed in, see docs for a list of available properties
    // https://firebase.google.com/docs/reference/js/firebase.User
    var uid = user.uid;
    // ...
  } else {
    // User is signed out
    // ...
  }
});

เมื่อใช้ผู้สังเกตการณ์ คุณจะมั่นใจได้ว่าอ็อบเจ็กต์ Auth ไม่ได้อยู่ในสถานะระดับกลาง เช่น การเริ่มต้น เมื่อคุณได้รับผู้ใช้ปัจจุบัน เมื่อคุณใช้ signInWithRedirect ผู้สังเกตการณ์ onAuthStateChanged จะรอจนกว่า getRedirectResult จะแก้ไขก่อนที่จะทริกเกอร์

คุณยังสามารถรับผู้ใช้ที่ลงชื่อเข้าใช้อยู่ในปัจจุบันได้โดยใช้คุณสมบัติ currentUser หากผู้ใช้ไม่ได้ลงชื่อเข้าใช้ currentUser จะเป็นโมฆะ:

Web version 9

import { getAuth } from "firebase/auth";

const auth = getAuth();
const user = auth.currentUser;

if (user) {
  // User is signed in, see docs for a list of available properties
  // https://firebase.google.com/docs/reference/js/firebase.User
  // ...
} else {
  // No user is signed in.
}

Web version 8

const user = firebase.auth().currentUser;

if (user) {
  // User is signed in, see docs for a list of available properties
  // https://firebase.google.com/docs/reference/js/firebase.User
  // ...
} else {
  // No user is signed in.
}

รับโปรไฟล์ผู้ใช้

หากต้องการรับข้อมูลโปรไฟล์ของผู้ใช้ ให้ใช้คุณสมบัติของอินสแตนซ์ User ตัวอย่างเช่น:

Web version 9

import { getAuth } from "firebase/auth";

const auth = getAuth();
const user = auth.currentUser;
if (user !== null) {
  // The user object has basic properties such as display name, email, etc.
  const displayName = user.displayName;
  const email = user.email;
  const photoURL = user.photoURL;
  const emailVerified = user.emailVerified;

  // The user's ID, unique to the Firebase project. Do NOT use
  // this value to authenticate with your backend server, if
  // you have one. Use User.getToken() instead.
  const uid = user.uid;
}

Web version 8

const user = firebase.auth().currentUser;
if (user !== null) {
  // The user object has basic properties such as display name, email, etc.
  const displayName = user.displayName;
  const email = user.email;
  const photoURL = user.photoURL;
  const emailVerified = user.emailVerified;

  // The user's ID, unique to the Firebase project. Do NOT use
  // this value to authenticate with your backend server, if
  // you have one. Use User.getIdToken() instead.
  const uid = user.uid;
}

รับข้อมูลโปรไฟล์เฉพาะผู้ให้บริการของผู้ใช้

ในการรับข้อมูลโปรไฟล์ที่ดึงมาจากผู้ให้บริการการลงชื่อเข้าใช้ที่เชื่อมโยงกับผู้ใช้ ให้ใช้คุณสมบัติ providerData ตัวอย่างเช่น:

Web version 9

import { getAuth } from "firebase/auth";

const auth = getAuth();
const user = auth.currentUser;

if (user !== null) {
  user.providerData.forEach((profile) => {
    console.log("Sign-in provider: " + profile.providerId);
    console.log("  Provider-specific UID: " + profile.uid);
    console.log("  Name: " + profile.displayName);
    console.log("  Email: " + profile.email);
    console.log("  Photo URL: " + profile.photoURL);
  });
}

Web version 8

const user = firebase.auth().currentUser;

if (user !== null) {
  user.providerData.forEach((profile) => {
    console.log("Sign-in provider: " + profile.providerId);
    console.log("  Provider-specific UID: " + profile.uid);
    console.log("  Name: " + profile.displayName);
    console.log("  Email: " + profile.email);
    console.log("  Photo URL: " + profile.photoURL);
  });
}

อัพเดทโปรไฟล์ของผู้ใช้

คุณสามารถอัปเดตข้อมูลโปรไฟล์พื้นฐานของผู้ใช้—ชื่อที่แสดงและ URL รูปโปรไฟล์—ด้วยวิธี updateProfile ตัวอย่างเช่น:

Web version 9

import { getAuth, updateProfile } from "firebase/auth";
const auth = getAuth();
updateProfile(auth.currentUser, {
  displayName: "Jane Q. User", photoURL: "https://example.com/jane-q-user/profile.jpg"
}).then(() => {
  // Profile updated!
  // ...
}).catch((error) => {
  // An error occurred
  // ...
});

Web version 8

const user = firebase.auth().currentUser;

user.updateProfile({
  displayName: "Jane Q. User",
  photoURL: "https://example.com/jane-q-user/profile.jpg"
}).then(() => {
  // Update successful
  // ...
}).catch((error) => {
  // An error occurred
  // ...
});  

ตั้งค่าที่อยู่อีเมลของผู้ใช้

คุณสามารถตั้งค่าที่อยู่อีเมลของผู้ใช้ด้วยวิธี updateEmail ตัวอย่างเช่น:

Web version 9

import { getAuth, updateEmail } from "firebase/auth";
const auth = getAuth();
updateEmail(auth.currentUser, "user@example.com").then(() => {
  // Email updated!
  // ...
}).catch((error) => {
  // An error occurred
  // ...
});

Web version 8

const user = firebase.auth().currentUser;

user.updateEmail("user@example.com").then(() => {
  // Update successful
  // ...
}).catch((error) => {
  // An error occurred
  // ...
});

ส่งอีเมลยืนยันให้ผู้ใช้

คุณสามารถส่งอีเมลยืนยันที่อยู่ให้กับผู้ใช้โดยใช้เมธอด sendEmailVerification ตัวอย่างเช่น:

Web version 9

import { getAuth, sendEmailVerification } from "firebase/auth";

const auth = getAuth();
sendEmailVerification(auth.currentUser)
  .then(() => {
    // Email verification sent!
    // ...
  });

Web version 8

firebase.auth().currentUser.sendEmailVerification()
  .then(() => {
    // Email verification sent!
    // ...
  });

คุณปรับแต่งเทมเพลตอีเมลที่ใช้ในส่วนการตรวจสอบสิทธิ์ของ คอนโซล Firebase ได้ในหน้าเทมเพลตอีเมล ดู เทมเพลตอีเมล ในศูนย์ช่วยเหลือของ Firebase

นอกจากนี้ยังสามารถส่งต่อสถานะผ่าน URL ดำเนิน การต่อเพื่อเปลี่ยนเส้นทางกลับไปที่แอปเมื่อส่งอีเมลยืนยัน

นอกจากนี้ คุณสามารถแปลอีเมลยืนยันโดยอัปเดตรหัสภาษาบนอินสแตนซ์ Auth ก่อนส่งอีเมล ตัวอย่างเช่น:

Web version 9

import { getAuth } from "firebase/auth";

const auth = getAuth();
auth.languageCode = 'it';
// To apply the default browser preference instead of explicitly setting it.
// firebase.auth().useDeviceLanguage();

Web version 8

firebase.auth().languageCode = 'it';
// To apply the default browser preference instead of explicitly setting it.
// firebase.auth().useDeviceLanguage();

ตั้งรหัสผ่านของผู้ใช้

คุณสามารถตั้งรหัสผ่านของผู้ใช้ด้วยวิธี updatePassword ตัวอย่างเช่น:

Web version 9

import { getAuth, updatePassword } from "firebase/auth";

const auth = getAuth();

const user = auth.currentUser;
const newPassword = getASecureRandomPassword();

updatePassword(user, newPassword).then(() => {
  // Update successful.
}).catch((error) => {
  // An error ocurred
  // ...
});

Web version 8

const user = firebase.auth().currentUser;
const newPassword = getASecureRandomPassword();

user.updatePassword(newPassword).then(() => {
  // Update successful.
}).catch((error) => {
  // An error ocurred
  // ...
});

ส่งอีเมลรีเซ็ตรหัสผ่าน

คุณสามารถส่งอีเมลรีเซ็ตรหัสผ่านไปยังผู้ใช้โดยใช้เมธอด sendPasswordResetEmail ตัวอย่างเช่น:

Web version 9

import { getAuth, sendPasswordResetEmail } from "firebase/auth";

const auth = getAuth();
sendPasswordResetEmail(auth, email)
  .then(() => {
    // Password reset email sent!
    // ..
  })
  .catch((error) => {
    const errorCode = error.code;
    const errorMessage = error.message;
    // ..
  });

Web version 8

firebase.auth().sendPasswordResetEmail(email)
  .then(() => {
    // Password reset email sent!
    // ..
  })
  .catch((error) => {
    var errorCode = error.code;
    var errorMessage = error.message;
    // ..
  });

คุณปรับแต่งเทมเพลตอีเมลที่ใช้ในส่วนการตรวจสอบสิทธิ์ของ คอนโซล Firebase ได้ในหน้าเทมเพลตอีเมล ดู เทมเพลตอีเมล ในศูนย์ช่วยเหลือของ Firebase

นอกจากนี้ยังสามารถส่งต่อสถานะผ่าน URL ดำเนิน การต่อเพื่อเปลี่ยนเส้นทางกลับไปที่แอปเมื่อส่งอีเมลรีเซ็ตรหัสผ่าน

นอกจากนี้ คุณสามารถแปลอีเมลรีเซ็ตรหัสผ่านโดยอัปเดตรหัสภาษาบนอินสแตนซ์ Auth ก่อนส่งอีเมล ตัวอย่างเช่น:

Web version 9

import { getAuth } from "firebase/auth";

const auth = getAuth();
auth.languageCode = 'it';
// To apply the default browser preference instead of explicitly setting it.
// firebase.auth().useDeviceLanguage();

Web version 8

firebase.auth().languageCode = 'it';
// To apply the default browser preference instead of explicitly setting it.
// firebase.auth().useDeviceLanguage();

คุณยังส่งอีเมลรีเซ็ตรหัสผ่านได้จากคอนโซล Firebase

ลบผู้ใช้

คุณสามารถลบบัญชีผู้ใช้ด้วยวิธีการ delete ตัวอย่างเช่น:

Web version 9

import { getAuth, deleteUser } from "firebase/auth";

const auth = getAuth();
const user = auth.currentUser;

deleteUser(user).then(() => {
  // User deleted.
}).catch((error) => {
  // An error ocurred
  // ...
});

Web version 8

const user = firebase.auth().currentUser;

user.delete().then(() => {
  // User deleted.
}).catch((error) => {
  // An error ocurred
  // ...
});

คุณยังสามารถลบผู้ใช้ออกจากส่วนการตรวจสอบสิทธิ์ของ คอนโซล Firebase บนหน้าผู้ใช้

ตรวจสอบสิทธิ์ผู้ใช้อีกครั้ง

การดำเนินการที่มีความละเอียดอ่อนด้านความปลอดภัยบางอย่าง เช่น การลบบัญชี การตั้งค่าที่อยู่อีเมลหลัก และ การเปลี่ยนรหัสผ่าน กำหนดให้ผู้ใช้เพิ่งลงชื่อเข้าใช้ หากคุณดำเนินการอย่างใดอย่างหนึ่งเหล่านี้ และผู้ใช้ลงชื่อเข้าใช้นานเกินไป การดำเนินการล้มเหลวโดยมีข้อผิดพลาด เมื่อสิ่งนี้เกิดขึ้น ให้ตรวจสอบสิทธิ์ผู้ใช้อีกครั้งโดยรับข้อมูลรับรองการลงชื่อเข้าใช้ใหม่จากผู้ใช้และส่งข้อมูลประจำตัวไปยัง reauthenticateWithCredential ตัวอย่างเช่น:

Web version 9

import { getAuth, reauthenticateWithCredential } from "firebase/auth";

const auth = getAuth();
const user = auth.currentUser;

// TODO(you): prompt the user to re-provide their sign-in credentials
const credential = promptForCredentials();

reauthenticateWithCredential(user, credential).then(() => {
  // User re-authenticated.
}).catch((error) => {
  // An error ocurred
  // ...
});

Web version 8

const user = firebase.auth().currentUser;

// TODO(you): prompt the user to re-provide their sign-in credentials
const credential = promptForCredentials();

user.reauthenticateWithCredential(credential).then(() => {
  // User re-authenticated.
}).catch((error) => {
  // An error occurred
  // ...
});

นำเข้าบัญชีผู้ใช้

คุณนำเข้าบัญชีผู้ใช้จากไฟล์ไปยังโปรเจ็กต์ Firebase ได้โดยใช้คำสั่ง auth:import ของ Firebase CLI ตัวอย่างเช่น:

firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14