ลิงก์ผู้ให้บริการการตรวจสอบสิทธิ์หลายรายกับบัญชีโดยใช้ JavaScript

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

ก่อนเริ่มต้น

เพิ่มการรองรับผู้ให้บริการตรวจสอบสิทธิ์อย่างน้อย 2 ราย (อาจรวมถึงการตรวจสอบสิทธิ์แบบไม่ระบุตัวตน) ลงในแอป

วิธีลิงก์ข้อมูลเข้าสู่ระบบจากผู้ให้บริการตรวจสอบสิทธิ์ เช่น Google หรือ Facebook กับบัญชีผู้ใช้ที่มีอยู่

  1. ลงชื่อเข้าใช้ผู้ใช้โดยใช้ผู้ให้บริการหรือวิธีการตรวจสอบสิทธิ์ใดก็ได้
  2. รับออบเจ็กต์ AuthProvider ที่สอดคล้องกับผู้ให้บริการที่ต้องการลิงก์กับบัญชีของผู้ใช้ ตัวอย่าง

    Web

    import { GoogleAuthProvider, FacebookAuthProvider, TwitterAuthProvider, GithubAuthProvider } from "firebase/auth";
    
    const googleProvider = new GoogleAuthProvider();
    const facebookProvider = new FacebookAuthProvider();
    const twitterProvider = new TwitterAuthProvider();
    const githubProvider = new GithubAuthProvider();

    Web

    var googleProvider = new firebase.auth.GoogleAuthProvider();
    var facebookProvider = new firebase.auth.FacebookAuthProvider();
    var twitterProvider = new firebase.auth.TwitterAuthProvider();
    var githubProvider = new firebase.auth.GithubAuthProvider();
  3. แจ้งให้ผู้ใช้ลงชื่อเข้าใช้ด้วยผู้ให้บริการที่ต้องการลิงก์ คุณสามารถแจ้งให้ผู้ใช้ลงชื่อเข้าใช้ได้โดยเปิดหน้าต่างป๊อปอัปหรือเปลี่ยนเส้นทางไปยังหน้าลงชื่อเข้าใช้ของผู้ให้บริการ ขอแนะนําให้ใช้วิธีการเปลี่ยนเส้นทางในอุปกรณ์เคลื่อนที่
    • หากต้องการลงชื่อเข้าใช้ด้วยหน้าต่างป๊อปอัป ให้โทรหา linkWithPopup

      Web

      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.
        // ...
      });

      Web

      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

      import { getAuth, linkWithRedirect, GoogleAuthProvider } from "firebase/auth";
      const provider = new GoogleAuthProvider();
      
      const auth = getAuth();
      linkWithRedirect(auth.currentUser, provider)
        .then(/* ... */)
        .catch(/* ... */);

      Web

      auth.currentUser.linkWithRedirect(provider)
        .then(/* ... */)
        .catch(/* ... */);
      หลังจากลงชื่อเข้าใช้แล้ว ระบบจะเปลี่ยนเส้นทางผู้ใช้กลับไปยังหน้าเว็บของคุณ จากนั้นคุณสามารถเรียกดูผลการลงชื่อเข้าใช้ได้โดยเรียกใช้ getRedirectResult เมื่อหน้าเว็บโหลด

      Web

      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.
        // ...
      });

      Web

      auth.getRedirectResult().then((result) => {
        if (result.credential) {
          // Accounts successfully linked.
          var credential = result.credential;
          var user = result.user;
          // ...
        }
      }).catch((error) => {
        // Handle Errors here.
        // ...
      });
    หากผู้ใช้ลงชื่อเข้าใช้สําเร็จ บัญชีของผู้ใช้กับผู้ให้บริการจะลิงก์กับบัญชีของผู้ใช้ในโปรเจ็กต์ Firebase

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

    Web

    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);
    });

    Web

    // 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);
    });

วิธีเพิ่มข้อมูลเข้าสู่ระบบอีเมลและรหัสผ่านลงในบัญชีผู้ใช้ที่มีอยู่

  1. ลงชื่อเข้าใช้ผู้ใช้โดยใช้ผู้ให้บริการหรือวิธีการตรวจสอบสิทธิ์ใดก็ได้
  2. แจ้งให้ผู้ใช้ป้อนอีเมลและรหัสผ่านใหม่
  3. สร้างออบเจ็กต์ AuthCredential ด้วยอีเมลและรหัสผ่าน

    Web

    import { EmailAuthProvider } from "firebase/auth";
    
    const credential = EmailAuthProvider.credential(email, password);

    Web

    var credential = firebase.auth.EmailAuthProvider.credential(email, password);
  4. ส่งออบเจ็กต์ AuthCredential ไปยังเมธอด linkWithCredential ของผู้ใช้ที่ลงชื่อเข้าใช้

    Web

    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);
      });

    Web

    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

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

const auth = getAuth();
unlink(auth.currentUser, providerId).then(() => {
  // Auth provider unlinked from account
  // ...
}).catch((error) => {
  // An error happened
  // ...
});

Web

user.unlink(providerId).then(() => {
  // Auth provider unlinked from account
  // ...
}).catch((error) => {
  // An error happened
  // ...
});