Liên kết nhiều nhà cung cấp xác thực với một tài khoản bằng JavaScript

Bạn có thể cho phép người dùng đăng nhập vào ứng dụng của mình bằng nhiều nhà cung cấp xác thực bằng cách liên kết thông tin xác thực của nhà cung cấp xác thực với tài khoản người dùng hiện có. Người dùng có thể được nhận dạng bằng cùng một ID người dùng Firebase bất kể nhà cung cấp xác thực mà họ đã sử dụng để đăng nhập. Ví dụ: người dùng đã đăng nhập bằng mật khẩu có thể liên kết tài khoản Google và đăng nhập bằng một trong hai phương pháp trong tương lai. Hoặc, người dùng ẩn danh có thể liên kết tài khoản Facebook và sau đó đăng nhập bằng Facebook để tiếp tục sử dụng ứng dụng của bạn.

Trước khi bắt đầu

Thêm hỗ trợ cho hai hoặc nhiều nhà cung cấp xác thực (có thể bao gồm xác thực ẩn danh) vào ứng dụng của bạn.

Để liên kết thông tin xác thực từ nhà cung cấp dịch vụ xác thực như Google hoặc Facebook với tài khoản người dùng hiện có:

  1. Đăng nhập người dùng bằng bất kỳ nhà cung cấp hoặc phương thức xác thực nào.
  2. Lấy đối tượng AuthProvider tương ứng với nhà cung cấp mà bạn muốn liên kết với tài khoản người dùng. Ví dụ:

    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. Nhắc người dùng đăng nhập với nhà cung cấp mà bạn muốn liên kết. Bạn có thể nhắc người dùng đăng nhập bằng cách mở cửa sổ bật lên hoặc bằng cách chuyển hướng đến trang đăng nhập của nhà cung cấp. Phương pháp chuyển hướng được ưa thích trên thiết bị di động.
    • Để đăng nhập bằng cửa sổ bật lên, hãy gọi 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.
        // ...
      });
    • Để đăng nhập bằng cách chuyển hướng đến trang đăng nhập của nhà cung cấp, hãy gọi linkWithRedirect : Thực hiện theo các phương pháp hay nhất khi sử dụng `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(/* ... */);
      Sau khi người dùng đăng nhập, họ sẽ được chuyển hướng trở lại trang của bạn. Sau đó, bạn có thể truy xuất kết quả đăng nhập bằng cách gọi getRedirectResult khi trang của bạn tải:

      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.
        // ...
      });
    Nếu người dùng đăng nhập thành công, tài khoản của người dùng với nhà cung cấp sẽ được liên kết với tài khoản của người dùng trong dự án Firebase của bạn.

    Liên kết tài khoản sẽ không thành công nếu thông tin đăng nhập đã được liên kết với tài khoản người dùng khác. Trong tình huống này, bạn phải xử lý việc hợp nhất các tài khoản và dữ liệu liên quan sao cho phù hợp với ứng dụng của mình:

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

Để thêm thông tin xác thực địa chỉ email và mật khẩu vào tài khoản người dùng hiện có:

  1. Đăng nhập người dùng bằng bất kỳ nhà cung cấp hoặc phương thức xác thực nào.
  2. Nhắc người dùng nhập địa chỉ email và mật khẩu mới.
  3. Tạo một đối tượng AuthCredential bằng địa chỉ email và mật khẩu:

    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. Truyền đối tượng AuthCredential tới phương thức linkWithCredential của người dùng đã đăng nhập:

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

    Cuộc gọi tới linkWithCredential sẽ không thành công nếu thông tin đăng nhập đã được liên kết với tài khoản người dùng khác. Trong trường hợp này, bạn phải xử lý việc hợp nhất các tài khoản và dữ liệu liên quan sao cho phù hợp với ứng dụng của mình (xem ví dụ ở trên).

Bạn có thể hủy liên kết nhà cung cấp dịch vụ xác thực khỏi tài khoản để người dùng không thể đăng nhập bằng nhà cung cấp đó nữa.

Để hủy liên kết nhà cung cấp xác thực khỏi tài khoản người dùng, hãy chuyển ID nhà cung cấp sang phương thức unlink . Bạn có thể lấy ID nhà cung cấp của các nhà cung cấp dịch vụ xác thực được liên kết với người dùng từ thuộc tính 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
  // ...
});