將多個供應商連結至一個帳戶

本文說明如何將多個提供者連結至單一 Identity Platform 帳戶。

Identity Platform 會使用專屬 ID 識別使用者。這樣一來,使用者就能透過不同的供應商登入同一個帳戶。舉例來說,使用者一開始用電話號碼註冊,之後可以連結 Google 帳戶,然後使用任一方法登入。

事前準備

在應用程式中新增支援兩個以上的識別資訊提供者。

啟用或停用帳戶連結

帳戶連結設定會決定 Identity Platform 如何處理使用者嘗試透過不同供應商,使用相同電子郵件登入的情況。

  • 連結使用相同電子郵件地址的帳戶:如果使用者嘗試以已使用的電子郵件地址登入,Identity Platform 會引發錯誤。應用程式可以擷取這項錯誤,並將新的供應商連結至現有帳戶。

  • 為每個識別資訊提供者建立多個帳戶:每次使用者透過不同的提供者登入時,系統都會建立新的 Identity Platform 使用者帳戶。

如何選擇設定:

  1. 前往Google Cloud 控制台的 Identity Platform「Settings」(設定) 頁面。

    前往「設定」頁面

  2. 在「使用者帳戶連結」下方選取設定。

  3. 按一下「儲存」。

連結聯合供應商憑證

如要連結聯合提供者的憑證,請按照下列步驟操作:

  1. 使用任何驗證供應商或方法讓使用者登入。

  2. 取得與要連結至使用者帳戶的提供者相對應的提供者物件。例如:

    網頁版 9

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

    網頁版 8

    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():

    網頁版 9

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

    網頁版 8

    auth.currentUser.linkWithPopup(provider).then((result) => {
      // Accounts successfully linked.
      var credential = result.credential;
      var user = result.user;
      // ...
    }).catch((error) => {
      // Handle Errors here.
      // ...
    });

    如要重新導向頁面,請先呼叫 linkWithRedirect():

    使用 signInWithRedirect、linkWithRedirect 或 reauthenticateWithRedirect 時,請遵循最佳做法。

    網頁版 9

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

    網頁版 8

    auth.currentUser.linkWithRedirect(provider)
      .then(/* ... */)
      .catch(/* ... */);

    使用者登入後,系統會將他們重新導向回您的應用程式。接著,您可以呼叫 getRedirectResult() 來擷取登入結果:

    網頁版 9

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

    網頁版 8

    auth.getRedirectResult().then((result) => {
      if (result.credential) {
        // Accounts successfully linked.
        var credential = result.credential;
        var user = result.user;
        // ...
      }
    }).catch((error) => {
      // Handle Errors here.
      // ...
    });

使用者在聯合供應商的帳戶現在已連結至其 Identity Platform 帳戶,且可以使用該供應商登入。

連結電子郵件和密碼憑證

如要在現有使用者帳戶中新增電子郵件地址和密碼,請按照下列步驟操作:

  1. 使用任何識別資訊提供者或方法登入使用者。

  2. 提示使用者輸入電子郵件地址和密碼。

  3. 使用電子郵件地址和密碼建立 AuthCredential 物件:

    網頁版 9

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

    網頁版 8

    var credential = firebase.auth.EmailAuthProvider.credential(email, password);
  4. 將 AuthCredential 物件傳遞至已登入使用者的 linkWithCredential() 方法:

    網頁版 9

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

    網頁版 8

    auth.currentUser.linkWithCredential(credential)
      .then((usercred) => {
        var user = usercred.user;
        console.log("Account linking success", user);
      }).catch((error) => {
        console.log("Account linking error", error);
      });

電子郵件地址和密碼憑證現在已連結至使用者的 Identity Platform 帳戶,使用者可以使用這些憑證登入。

請注意,聯合提供者憑證可以連結至使用不同電子郵件的電子郵件/密碼帳戶。如果發生這種情況,可以使用與聯合供應商對應的電子郵件,建立獨立的電子郵件/密碼帳戶。

處理 account-exists-with-different-credential 錯誤

如果您在Google Cloud 控制台中啟用「Link accounts that use the same email」(連結使用相同電子郵件的帳戶) 設定,當使用者嘗試使用其他供應商 (例如 Google) 已有的電子郵件登入供應商時,系統會傳回 auth/account-exists-with-different-credential 錯誤和 AuthCredential 物件,導致登入失敗。

如要處理這項錯誤,請提示使用者使用現有供應商登入。接著,呼叫 linkWithCredential()、linkWithPopup() 或 linkWithRedirect(),使用 AuthCredential 將新供應商與其帳戶建立關聯。

以下範例說明如何在使用者嘗試使用 Facebook 登入時處理這項錯誤:

網頁版 9

  import { signInWithPopup, signInWithEmailAndPassword, linkWithCredential, FacebookAuthProvider } from "firebase/auth";

  // User tries to sign in with Facebook.
  signInWithPopup(auth, facebookProvider).catch((error) => {
  // User's email already exists.
  if (error.code === 'auth/account-exists-with-different-credential') {
    // The pending Facebook credential.
    const pendingCred = FacebookAuthProvider.credentialFromError(error);
    // The provider account's email address.
    const email = error.customData.email;

    // Present the user with a list of providers they might have
    // used to create the original account.
    // Then, ask the user to sign in with the existing provider.
    const method = promptUserForSignInMethod();

    if (method === 'password') {
      // TODO: Ask the user for their password.
      // In real scenario, you should handle this asynchronously.
      const password = promptUserForPassword();
      signInWithEmailAndPassword(auth, email, password).then((result) => {
        return linkWithCredential(result.user, pendingCred);
      }).then(() => {
        // Facebook account successfully linked to the existing user.
        goToApp();
      });
      return;
    }

    // All other cases are external providers.
    // Construct provider object for that provider.
    // TODO: Implement getProviderForProviderId.
    const provider = getProviderForProviderId(method);
    // At this point, you should let the user know that they already have an
    // account with a different provider, and validate they want to sign in
    // with the new provider.
    // Note: Browsers usually block popups triggered asynchronously, so in
    // real app, you should ask the user to click on a "Continue" button
    // that will trigger signInWithPopup().
    signInWithPopup(auth, provider).then((result) => {
      // Note: Identity Platform doesn't control the provider's sign-in
      // flow, so it's possible for the user to sign in with an account
      // with a different email from the first one.

      // Link the Facebook credential. We have access to the pending
      // credential, so we can directly call the link method.
      linkWithCredential(result.user, pendingCred).then((userCred) => {
        // Success.
        goToApp();
      });
    });
  }
});

網頁版 8

  // User tries to sign in with Facebook.
      auth.signInWithPopup(facebookProvider).catch((error) => {
  // User's email already exists.
  if (error.code === 'auth/account-exists-with-different-credential') {
    // The pending Facebook credential.
    const pendingCred = error.credential;
    // The provider account's email address.
    const email = error.email;

    // Present the user with a list of providers they might have
    // used to create the original account.
    // Then, ask the user to sign in with the existing provider.
    const method = promptUserForSignInMethod();

    if (method === 'password') {
      // TODO: Ask the user for their password.
      // In real scenario, you should handle this asynchronously.
      const password = promptUserForPassword();
      auth.signInWithEmailAndPassword(email, password).then((result) => {
        return result.user.linkWithCredential(pendingCred);
      }).then(() => {
        // Facebook account successfully linked to the existing user.
        goToApp();
      });
      return;
    }

    // All other cases are external providers.
    // Construct provider object for that provider.
    // TODO: Implement getProviderForProviderId.
    const provider = getProviderForProviderId(method);
    // At this point, you should let the user know that they already have an
    // account with a different provider, and validate they want to sign in
    // with the new provider.
    // Note: Browsers usually block popups triggered asynchronously, so in
    // real app, you should ask the user to click on a "Continue" button
    // that will trigger signInWithPopup().
    auth.signInWithPopup(provider).then((result) => {
      // Note: Identity Platform doesn't control the provider's sign-in
      // flow, so it's possible for the user to sign in with an account
      // with a different email from the first one.

      // Link the Facebook credential. We have access to the pending
      // credential, so we can directly call the link method.
      result.user.linkWithCredential(pendingCred).then((userCred) => {
        // Success.
        goToApp();
      });
    });
  }
});

使用重新導向與彈出式視窗類似,但您必須在頁面重新導向之間快取待處理的憑證 (例如使用工作階段儲存空間)。

請注意,部分提供者 (例如 Google 和 Microsoft) 同時提供電子郵件和社群識別資訊。電子郵件服務供應商對代管電子郵件網域相關的所有地址,具有權威性。也就是說,如果使用者登入時使用的電子郵件地址是由同一個供應商託管,就不會發生這項錯誤 (例如使用 @gmail.com 電子郵件登入 Google,或使用 @live.com 或 @outlook.com 電子郵件登入 Microsoft)。

使用信任的提供者 (例如 SAML) 登入,不會導致 auth/account-exists-with-different-credential 錯誤。 如果現有帳戶已驗證電子郵件,Identity Platform 會自動將新的提供者連結至現有帳戶,並保留現有憑證。如果現有帳戶的電子郵件未經驗證 (例如未經驗證的電子郵件或密碼帳戶),信任的供應商會覆寫未經驗證的帳戶,並移除現有密碼和未經驗證的供應商憑證,防止帳戶遭預先入侵。

手動合併帳戶

如果使用者嘗試使用已連結至其他使用者帳戶的憑證登入,且該帳戶使用相同的供應商,則用戶端 SDK 的內建帳戶連結方法將會失敗。在這種情況下,您必須手動合併帳戶,然後刪除第二個帳戶。例如:

網頁版 9

// Sign in first account.
const result1 = await signInWithCredential(auth, cred1);
const user1 = result1.user;
// Try to link a credential that belongs to an existing account
try {
  await linkWithCredential(user1, cred2);
} catch (error) {
  // cred2 already exists so an error is thrown.
  const result2 = await signInWithCredential(auth, error.credential);
  const user2 = result2.user;
  // Merge the data.
  mergeData(user1, user2);
  // Delete one of the accounts, and try again.
  await user2.delete();
  // Linking now will work.
  await linkWithCredential(user1, result2.credential);
}

網頁版 8

// Sign in first account.
const result1 = await auth.signInWithCredential(cred1);
const user1 = result1.user;
// Try to link a credential that belongs to an existing account
try {
  await user1.linkWithCredential(cred2);
} catch (error) {
  // cred2 already exists so an error is thrown.
  const result2 = await auth.signInWithCredential(error.credential);
  const user2 = result2.user;
  // Merge the data.
  mergeData(user1, user2);
  // Delete one of the accounts, and try again.
  await user2.delete();
  // Linking now will work.
  await user1.linkWithCredential(result2.credential);
}

您可以取消連結供應商與使用者帳戶。使用者將無法再透過該供應商進行驗證。

如要取消連結供應商,請將供應商 ID 傳遞至 unlink() 方法。您可以從 providerData 屬性取得與使用者連結的驗證供應商 ID。

網頁版 9

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

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

網頁版 8

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

後續步驟