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

สร้างผู้ใช้

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

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

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

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

Web modular API

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/auth.user
    const uid = user.uid;
    // ...
  } else {
    // User is signed out
    // ...
  }
});

Web namespaced API

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/v8/firebase.User
    var uid = user.uid;
    // ...
  } else {
    // User is signed out
    // ...
  }
});

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

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

Web modular API

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/auth.user
  // ...
} else {
  // No user is signed in.
}

Web namespaced API

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/v8/firebase.User
  // ...
} else {
  // No user is signed in.
}

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

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

Web modular API

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 namespaced API

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 modular API

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 namespaced API

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 modular API

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 namespaced API

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 modular API

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

Web namespaced API

const user = firebase.auth().currentUser;

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

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

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

Web modular API

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

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

Web namespaced API

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

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

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

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

Web modular API

import { getAuth } from "firebase/auth";

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

Web namespaced API

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

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

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

Web modular API

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 namespaced API

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

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

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

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

Web modular API

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 namespaced API

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

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

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

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

Web modular API

import { getAuth } from "firebase/auth";

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

Web namespaced API

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

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

ลบผู้ใช้

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

Web modular API

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

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

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

Web namespaced API

const user = firebase.auth().currentUser;

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

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

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

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

Web modular API

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 namespaced API

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