英文:
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 版本中已被弃用。你可以尝试使用 OAuthProvider 的 credentialFromError 方法,你会得到类似这样的结果:
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论