สร้างผู้ใช้
คุณสร้างผู้ใช้ใหม่ในโครงการ 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 ดำเนินการต่อ เพื่อเปลี่ยนเส้นทางกลับไปที่แอปเมื่อส่งอีเมลยืนยัน
นอกจากนี้ คุณสามารถแปลอีเมลยืนยันเป็นภาษาท้องถิ่นได้โดยอัปเดตรหัสภาษาในอินสแตนซ์ 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();
ตั้งรหัสผ่านของผู้ใช้
คุณสามารถตั้งรหัสผ่านของผู้ใช้ด้วยวิธี 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
ด้วยข้อมูลประจำตัวอีกครั้ง ตัวอย่างเช่น:
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