在这种情况下,我想处理 linkWithPopup 和 Firebase `auth/credential-already-in-use` 错误

Kid*_*Kid 6 firebase reactjs firebase-authentication

我阅读了使用 JavaScript 进行身份验证,但我看不到错误代码的解决方案auth/credential-already-in-use

基本上我尝试 linkWithPopup 但用户之前已经在另一个浏览器中登录,所以我无法链接他,所以我可以简单地在这个浏览器中登录他,不需要链接。

这就是我收到此错误的原因auth/credential-already-in-use

我有这个代码:(
firebase.auth.currentUser是一个匿名登录用户)

onSocialLoginLink = provider => {
    const { firebase, changeUserRole } = this.props;
    firebase.auth.currentUser
        .linkWithPopup(firebase[provider])
        .then(res => {
            if (res.credential) {
                // do stuff;
            }
        })
        .catch(error => {
            if (error.code == 'auth/credential-already-in-use') {
                // Here I want to directly sign in with this credential 
                console.log('linkWithProvider Failure, error', error);
                console.log('linkWithProvider Failure, error["credential"]', error['credential']);
            } else {
                this.setState({ error });
            }
        });
};
Run Code Online (Sandbox Code Playgroud)

我是这方面的初学者,我在文档中找不到如何做到这一点。在执行此操作之前我是否必须先注销匿名用户signInWithPopup?是否可以在没有用户交互的情况下做到这一点signInWithPopup

当我在里面时,.catch(error =...我知道 Gmail 之类的证明以及特定用户的 Gmail 地址。我从error.credential

或者这可能是一个糟糕的解决方案,请提出建议?

Sum*_*ron 4

我自己也有同样的问题。谢谢你的慰问。我刚刚查看了文档,发现其中指出:

auth/credential-already-in-use如果与该凭据对应的帐户已存在于您的用户中,或者已链接到 Firebase 用户,则抛出该错误。例如,如果您通过将 Google 凭据链接到匿名用户将其升级为 Google 用户,并且所使用的 Google 凭据已与现有 Firebase Google 用户关联,则可能会引发此错误。还提供了 error.email 和 error.credential (firebase.auth.AuthCredential) 字段。您可以通过直接通过 firebase.auth.Auth.signInWithCredential 使用该凭据登录来恢复此错误。

当然,相关链接是firebase.auth.Auth.signInWithCredential

我刚刚在我自己的代码中尝试了这个,看起来像这样

try {
  // logged in with an anon user
  // try to connect via popup with 3rd part
} catch (e) {
  // if code is auth already in use
  firebase.auth.Auth.signInWithCredential(e.credential)
} finally {
  // clean up
}

Run Code Online (Sandbox Code Playgroud)