【问题标题】:How can I solve create account problem with different providers?如何解决创建不同提供商的帐户问题?
【发布时间】:2022-10-16 03:05:11
【问题描述】:

我有一个登录谷歌:example@gmail.com

然后创建一个相同的帐户电子邮件:example@gmail.com

两个不同的提供商有问题

  1. 使用 Google 登录(相同的 Gmail)
  2. 使用电子邮件登录(相同的 Gmail)

    我该如何处理这两个(当我从 Firebase 控制台删除 google 登录帐户时。我可以使用该电子邮件创建一个帐户) 否则我无法使用该电子邮件创建帐户,也无法登录。

    我通过https://github.com/gladly-team/next-firebase-auth 学习 Firebase 身份验证

【问题讨论】:

    标签: firebase google-cloud-platform firebase-authentication google-authentication


    【解决方案1】:

    如果您首先使用“example@gmail.com”通过 Google 登录,则意味着将使用此特定电子邮件地址创建用户。如果您尝试使用任何其他提供商或使用电子邮件和密码登录相同的电子邮件地址,您将收到一条错误消息,指出该用户已存在。这是有道理的,因为您之前已经为用户使用过该电子邮件。

    有两种方法可以解决此问题。当您收到此类错误时,您可以检查用于创建帐户的提供程序,并通知用户使用它。例如,如果用户使用 Google 登录并尝试在此之后立即使用电子邮件和密码进行身份验证,则向用户显示一条消息,您应该在其中说用户已经存在,并且应该使用选择的身份验证提供程序来创建首先是帐户,在本例中为 Google。

    第二种选择是允许用户拥有多个使用不同身份验证提供商的电子邮件地址的帐户。此选项可以直接在 Firebase Console 的身份验证部分中启用。

    因此,由您决定哪个选项更适合您的项目。

    【讨论】:

    • 我对 firebase Auth 有一些问题,我有条件渲染登录组件和仪表板组件。我想我需要在 cookies 中存储一些密钥。我的问题是刷新页面时,用户登录但可以看到登录组件一秒钟。我需要饼干吗?我该如何修复它。我使用 Next js
    • 如果没有看到您正在使用的代码,我将无法提供太多帮助。如果您在实施该机制时遇到困难,请在 StackOverflow 上发布一个新问题,使用它自己的 MCVE,以便我和其他 Firebase 开发人员可以帮助您。
    • 我可以帮助您提供有关初始问题的其他信息吗?
    • 我已经问过那个问题了。仍然需要帮助
    • 你看过我之前的评论吗?你问过另一个问题吗?我在问我是否可以为您提供有关初始问题的其他信息,而不是最新问题?
    【解决方案2】:

    您将必须链接该帐户。

    这是一个带有特定电子邮件的 facebook 帐户的示例 并且您想使用同一电子邮件使用电子邮件和密码登录,如果这两个电子邮件未链接,则会出现不同的提供商错误。查看here了解更多信息

     export function linkFaceBookAccount(authContext?: AuthContextType, notificationContext?: NotificationContextType, history?: History.History) {
    const provider = new FacebookAuthProvider(); // create a provider
    
    linkWithPopup(auth.currentUser as User, provider).then((result) => {
        // This gives you a Google Access Token. You can use it to access the Google API.
        // const credential = FacebookAuthProvider.credentialFromResult(result);
        // const token = credential?.accessToken;
        // The signed-in user info.
        const user = result.user;
        saveUserToLocalStorage(user);
        authContext?.loadUserToState(user);
    
        notificationContext?.addNotification({
            message: `This email's (${auth.currentUser?.email}) account has been successful linked with your facebook account`,
            title: "Link successful",
            notificationType: "SUCCESS",
            positiveActionText: "continue",
            positiveAction: () => {
                history?.push("/")
            }
        })
    }).catch((error) => {
    
        const email = error.customData?.email;
        const errorCode = error.code;
        const duplicateAccount = errorCode === "auth/account-exists-with-different-credential";
        notificationContext?.addNotification({
            message: errorFirebase(error, email),
            title: "Linking Error",
            notificationType: "WARNING",
            positiveActionText: duplicateAccount ? "Link" : "ok",
            negativeActionText: duplicateAccount ? "cancel" : undefined,
            code: errorCode,
            positiveAction: () => {
                if (duplicateAccount) {
                    duplicateAccountLinking(email, "FACEBOOK", history);
                }
            }
        })
    });}
    

    【讨论】:

      猜你喜欢
      • 2016-11-20
      • 2018-07-20
      • 2022-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 2020-02-26
      相关资源
      最近更新 更多