Firebase `error.credential`在匿名身份验证回退时应存在,但实际上并不存在。

huangapple go评论59阅读模式
英文:

Firebase `error.credential` does not exist when it should for anonymous auth fallback

问题

使用`linkWithPopup`与Firebase一起使用时,当出现诸如`auth/credential-already-in-use`等错误时,`error.credential`不存在,这正是能够使用`error.credential`进行回退并执行正常登录的整个目的。

此代码始终为`error.credential`生成未定义值。
英文:

error.credential does not exist when using linkWithPopup with firebase when getting errors such as auth/credential-already-in-use -- which is the whole point of being able to use error.credential to fall back and perform a normal login

linkWithPopup(auth.currentUser, googleAuthProvider)
        .then((result) => {
            const user = result.user;
            console.log("Anonymous account successfully upgraded", user);
            window.location = redirectUrl;
        }).catch((error) => {
            console.log("Error upgrading anonymous account", error);
            console.log(error.credential);
            if (error.credential) {
                signInWithCredential(auth, error.credential)
                    .then((result) => {
                        const user = result.user;
                        console.log("recovered");
                    })
                    .catch((error) => {
                        console.log("error in google auth recovery", errorMessage);
                    });
            }
        });

this code always produces undefined for error.credential

答案1

得分: 1

我遇到过同样的问题,error.credential 在 Firebase 的 v9 版本中已被弃用。你可以尝试使用 OAuthProvidercredentialFromError 方法,你会得到类似这样的结果:

import {
  OAuthProvider,
} from 'firebase/auth';

linkWithPopup(auth.currentUser, googleAuthProvider).catch(error) {
  const authCredential = await OAuthProvider.credentialFromError(error);
}
英文:

I had the same issue, the error.credential was deprecated in v9 version of Firebase.
So, you can try to use credentialFromError method of OAuthProvider. And you will get something like this:

import {
  OAuthProvider,
} from 'firebase/auth'

linkWithPopup(auth.currentUser, googleAuthProvider).catch(error) {
  const authCredential = await OAuthProvider.credentialFromError(error)
}

huangapple
  • 本文由 发表于 2023年5月20日 22:15:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定