เชื่อมโยงผู้ให้บริการรับรองความถูกต้องหลายรายเข้ากับบัญชีโดยใช้ JavaScript

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

ก่อนที่คุณจะเริ่ม

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

หากต้องการเชื่อมโยงข้อมูลรับรองจากผู้ให้บริการรับรองความถูกต้อง เช่น Google หรือ Facebook ไปยังบัญชีผู้ใช้ที่มีอยู่:

  1. ลงชื่อเข้าใช้ผู้ใช้โดยใช้ผู้ให้บริการหรือวิธีการตรวจสอบความถูกต้อง
  2. รับออบเจ็กต์ 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();

    Web namespaced 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();
  3. แจ้งให้ผู้ใช้ลงชื่อเข้าใช้กับผู้ให้บริการที่คุณต้องการลิงก์ คุณสามารถแจ้งให้ผู้ใช้ลงชื่อเข้าใช้ได้โดยเปิดหน้าต่างป๊อปอัปหรือเปลี่ยนเส้นทางไปยังหน้าลงชื่อเข้าใช้ของผู้ให้บริการ วิธีการเปลี่ยนเส้นทางเป็นที่ต้องการบนอุปกรณ์เคลื่อนที่
    • หากต้องการลงชื่อเข้าใช้ด้วยหน้าต่างป๊อปอัป ให้โทรไปที่ 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.
        // ...
      });

      Web namespaced 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(/* ... */);

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

      Web namespaced API

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

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

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

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

    Web modular API

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

    Web namespaced API

    var credential = firebase.auth.EmailAuthProvider.credential(email, password);
  4. ส่งผ่านออบเจ็กต์ 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);
      });

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

Web namespaced API

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