คุณอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปของคุณโดยใช้ผู้ให้บริการการตรวจสอบสิทธิ์หลายรายได้โดยลิงก์ข้อมูลเข้าสู่ระบบของผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่ ผู้ใช้จะระบุตัวผู้ใช้ได้ด้วยรหัสผู้ใช้ Firebase เดียวกัน โดยไม่คำนึงถึงผู้ให้บริการตรวจสอบสิทธิ์ที่ใช้ลงชื่อเข้าใช้ เช่น ผู้ใช้ที่ลงชื่อเข้าใช้ด้วยรหัสผ่านสามารถลิงก์บัญชี Google และลงชื่อเข้าใช้ด้วยวิธีใดวิธีหนึ่งในอนาคต หรือผู้ใช้ที่ไม่ระบุชื่อสามารถเชื่อมโยงบัญชี Facebook แล้วลงชื่อเข้าใช้ด้วย Facebook ในภายหลังเพื่อใช้แอปของคุณต่อไป
ก่อนเริ่มต้น
เพิ่มการรองรับผู้ให้บริการการตรวจสอบสิทธิ์ 2 รายขึ้นไป (อาจรวมถึงการตรวจสอบสิทธิ์แบบไม่ระบุตัวตน) ในแอป
ลิงก์ข้อมูลเข้าสู่ระบบของผู้ให้บริการการตรวจสอบสิทธิ์แบบรวมศูนย์กับบัญชีผู้ใช้
หากต้องการลิงก์ข้อมูลเข้าสู่ระบบจากผู้ให้บริการตรวจสอบสิทธิ์ เช่น Google หรือ Facebook กับบัญชีผู้ใช้ที่มีอยู่ ให้ทำดังนี้
- ลงชื่อเข้าใช้ผู้ใช้โดยใช้ผู้ให้บริการหรือวิธีการตรวจสอบสิทธิ์ใดก็ได้
- รับออบเจ็กต์
AuthProvider
ที่สอดคล้องกับผู้ให้บริการที่คุณต้องการลิงก์กับบัญชีของผู้ใช้ ตัวอย่าง:Web Modular API
import { GoogleAuthProvider, FacebookAuthProvider, TwitterAuthProvider, GithubAuthProvider } from "firebase/auth"; const googleProvider = new GoogleAuthProvider(); const facebookProvider = new FacebookAuthProvider(); const twitterProvider = new TwitterAuthProvider(); const githubProvider = new GithubAuthProvider();
API ที่ใช้เนมสเปซในเว็บ
var googleProvider = new firebase.auth.GoogleAuthProvider(); var facebookProvider = new firebase.auth.FacebookAuthProvider(); var twitterProvider = new firebase.auth.TwitterAuthProvider(); var githubProvider = new firebase.auth.GithubAuthProvider();
- แจ้งให้ผู้ใช้ลงชื่อเข้าใช้ด้วยผู้ให้บริการที่ต้องการลิงก์ คุณแจ้งให้ผู้ใช้ลงชื่อเข้าใช้ได้โดยการเปิดหน้าต่างป๊อปอัปหรือเปลี่ยนเส้นทางไปยังหน้าลงชื่อเข้าใช้ของผู้ให้บริการ ขอแนะนำให้ใช้วิธีเปลี่ยนเส้นทางในอุปกรณ์เคลื่อนที่
- หากต้องการลงชื่อเข้าใช้ด้วยหน้าต่างป๊อปอัป โปรดโทรหา
linkWithPopup
Web Modular API
import { getAuth, linkWithPopup, GoogleAuthProvider } from "firebase/auth"; const provider = new GoogleAuthProvider(); const auth = getAuth(); linkWithPopup(auth.currentUser, provider).then((result) => { // Accounts successfully linked. const credential = GoogleAuthProvider.credentialFromResult(result); const user = result.user; // ... }).catch((error) => { // Handle Errors here. // ... });
API ที่ใช้เนมสเปซในเว็บ
auth.currentUser.linkWithPopup(provider).then((result) => { // Accounts successfully linked. var credential = result.credential; var user = result.user; // ... }).catch((error) => { // Handle Errors here. // ... });
- หากต้องการลงชื่อเข้าใช้โดยการเปลี่ยนเส้นทางไปยังหน้าลงชื่อเข้าใช้ของผู้ให้บริการ โปรดโทรหา
linkWithRedirect
โดยทำดังนี้ ทำตามแนวทางปฏิบัติแนะนำเมื่อใช้ "linkWithRedirect"Web Modular API
import { getAuth, linkWithRedirect, GoogleAuthProvider } from "firebase/auth"; const provider = new GoogleAuthProvider(); const auth = getAuth(); linkWithRedirect(auth.currentUser, provider) .then(/* ... */) .catch(/* ... */);
API ที่ใช้เนมสเปซในเว็บ
auth.currentUser.linkWithRedirect(provider) .then(/* ... */) .catch(/* ... */);
getRedirectResult
เมื่อโหลดหน้าเว็บ:Web Modular API
import { getRedirectResult } from "firebase/auth"; getRedirectResult(auth).then((result) => { const credential = GoogleAuthProvider.credentialFromResult(result); if (credential) { // Accounts successfully linked. const user = result.user; // ... } }).catch((error) => { // Handle Errors here. // ... });
API ที่ใช้เนมสเปซในเว็บ
auth.getRedirectResult().then((result) => { if (result.credential) { // Accounts successfully linked. var credential = result.credential; var user = result.user; // ... } }).catch((error) => { // Handle Errors here. // ... });
การลิงก์บัญชีจะไม่สำเร็จหากข้อมูลเข้าสู่ระบบลิงก์กับบัญชีผู้ใช้อื่นอยู่แล้ว ในกรณีนี้ คุณต้องจัดการการผสานรวมบัญชีและข้อมูลที่เกี่ยวข้องตามความเหมาะสมสำหรับแอป
Web Modular API
import { getAuth, signInWithCredential, linkWithCredential, OAuthProvider } from "firebase/auth"; // The implementation of how you store your user data depends on your application const repo = new MyUserDataRepo(); // Get reference to the currently signed-in user const auth = getAuth(); const prevUser = auth.currentUser; // Get the data which you will want to merge. This should be done now // while the app is still signed in as this user. const prevUserData = repo.get(prevUser); // Delete the user's data now, we will restore it if the merge fails repo.delete(prevUser); // Sign in user with the account you want to link to signInWithCredential(auth, newCredential).then((result) => { console.log("Sign In Success", result); const currentUser = result.user; const currentUserData = repo.get(currentUser); // Merge prevUser and currentUser data stored in Firebase. // Note: How you handle this is specific to your application const mergedData = repo.merge(prevUserData, currentUserData); const credential = OAuthProvider.credentialFromResult(result); return linkWithCredential(prevUser, credential) .then((linkResult) => { // Sign in with the newly linked credential const linkCredential = OAuthProvider.credentialFromResult(linkResult); return signInWithCredential(auth, linkCredential); }) .then((signInResult) => { // Save the merged data to the new user repo.set(signInResult.user, mergedData); }); }).catch((error) => { // If there are errors we want to undo the data merge/deletion console.log("Sign In Error", error); repo.set(prevUser, prevUserData); });
API ที่ใช้เนมสเปซในเว็บ
// The implementation of how you store your user data depends on your application var repo = new MyUserDataRepo(); // Get reference to the currently signed-in user var prevUser = auth.currentUser; // Get the data which you will want to merge. This should be done now // while the app is still signed in as this user. var prevUserData = repo.get(prevUser); // Delete the user's data now, we will restore it if the merge fails repo.delete(prevUser); // Sign in user with the account you want to link to auth.signInWithCredential(newCredential).then((result) => { console.log("Sign In Success", result); var currentUser = result.user; var currentUserData = repo.get(currentUser); // Merge prevUser and currentUser data stored in Firebase. // Note: How you handle this is specific to your application var mergedData = repo.merge(prevUserData, currentUserData); return prevUser.linkWithCredential(result.credential) .then((linkResult) => { // Sign in with the newly linked credential return auth.signInWithCredential(linkResult.credential); }) .then((signInResult) => { // Save the merged data to the new user repo.set(signInResult.user, mergedData); }); }).catch((error) => { // If there are errors we want to undo the data merge/deletion console.log("Sign In Error", error); repo.set(prevUser, prevUserData); });
- หากต้องการลงชื่อเข้าใช้ด้วยหน้าต่างป๊อปอัป โปรดโทรหา
ลิงก์อีเมลและรหัสผ่านกับบัญชีผู้ใช้
วิธีเพิ่มข้อมูลเข้าสู่ระบบอีเมลและรหัสผ่านลงในบัญชีผู้ใช้ที่มีอยู่
- ลงชื่อเข้าใช้ผู้ใช้โดยใช้ผู้ให้บริการหรือวิธีการตรวจสอบสิทธิ์ใดก็ได้
- แจ้งให้ผู้ใช้ทราบอีเมลและรหัสผ่านใหม่
- สร้างออบเจ็กต์
AuthCredential
ด้วยอีเมลและรหัสผ่าน ดังนี้Web Modular API
import { EmailAuthProvider } from "firebase/auth"; const credential = EmailAuthProvider.credential(email, password);
API ที่ใช้เนมสเปซในเว็บ
var credential = firebase.auth.EmailAuthProvider.credential(email, password);
ส่งออบเจ็กต์
AuthCredential
ไปยังเมธอดlinkWithCredential
ของผู้ใช้ที่ลงชื่อเข้าใช้ ดังนี้Web Modular API
import { getAuth, linkWithCredential } from "firebase/auth"; const auth = getAuth(); linkWithCredential(auth.currentUser, credential) .then((usercred) => { const user = usercred.user; console.log("Account linking success", user); }).catch((error) => { console.log("Account linking error", error); });
API ที่ใช้เนมสเปซในเว็บ
auth.currentUser.linkWithCredential(credential) .then((usercred) => { var user = usercred.user; console.log("Account linking success", user); }).catch((error) => { console.log("Account linking error", error); });
การเรียก
linkWithCredential
จะล้มเหลวหากข้อมูลเข้าสู่ระบบลิงก์กับบัญชีผู้ใช้อื่นอยู่แล้ว ในกรณีนี้ คุณต้องจัดการการผสานรวมบัญชีและข้อมูลที่เกี่ยวข้องตามความเหมาะสมสำหรับแอป (ดูตัวอย่างด้านบน)
ยกเลิกการลิงก์ผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้
คุณยกเลิกการลิงก์ผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีได้ เพื่อไม่ให้ผู้ใช้ลงชื่อเข้าใช้กับผู้ให้บริการรายนั้นได้อีก
หากต้องการยกเลิกการลิงก์ผู้ให้บริการตรวจสอบสิทธิ์จากบัญชีผู้ใช้ ให้ส่งรหัสผู้ให้บริการไปยังเมธอด unlink
คุณรับรหัสผู้ให้บริการของผู้ให้บริการการตรวจสอบสิทธิ์ที่ลิงก์กับผู้ใช้ได้จากพร็อพเพอร์ตี้ providerData
Web Modular API
import { getAuth, unlink } from "firebase/auth"; const auth = getAuth(); unlink(auth.currentUser, providerId).then(() => { // Auth provider unlinked from account // ... }).catch((error) => { // An error happened // ... });
API ที่ใช้เนมสเปซในเว็บ
user.unlink(providerId).then(() => { // Auth provider unlinked from account // ... }).catch((error) => { // An error happened // ... });