javascript - 尝试在 firebase 中链接多个身份验证提供程序时,从 error.email 获取 "undefined"

标签 javascript firebase react-native firebase-authentication expo

我正在尝试使用 firebase 将多个身份验证提供程序链接到一个帐户。用户正在尝试创建一个与 firebase 上已存在的 Google OAuth 帐户地址相同的帐户。


firebase
.auth()
.createUserWithEmailAndPassword(email, password)
.then(async result => {
    if (result.additionalUserInfo.isNewUser) {
        firebase
            .database()
            .ref('/users/' + result.user.uid)
            .set({
                email: email,
                profile_picture: image,
                first_name: firstName,
                last_name: lastName,
                created_at: Date.now()
            })
            .then(snapshot => console.log("do something"))
    } else {
        firebase
            .database()
            .ref('/users/' + result.user.uid)
            .update({
                last_logged_in: Date.now()
            })
            .then(snapshot => console.log("do something"))
    }
})
.catch(error => {
    if (error.code === 'auth/email-already-in-use' || error.code === 'auth/credential-already-in-use' || error.code === 'auth/account-exists-with-different-credential') {
        const pendingCred = error.credential
        const email = error.email
        firebase
            .auth()
            .fetchSignInMethodsForEmail(email)
            .then(methods => {
                switch (methods[0]) {
                    case 'password':
                        // email and password logic
                        break;
                    case 'facebook.com':
                        // facebook logic
                        break;
                    default:
                        break;
                }
            })
        return;
    }
})

问题是我收到了正确的错误消息:

[Error: The email address is already in use by another account.]

和正确的error.code:

auth/email-already-in-use

但是,pendingCrederror.email 返回未定义

更新
我接受了建议并尝试了以下方法:

firebase.auth()
    .EmailAuthProvider
    .credential(email, password)
    .then(result => console.log("result", result))
    .catch(error => console.log(error))

我收到错误:

[TypeError: undefined is not an object (evaluating '_firebase.default.auth().EmailAuthProvider.credential')]

最佳答案

您使用的 createuserwithEmailAndPassword 不包含 error.emailerror.credential。根据文档获取错误,您可以使用 error.messageerror.code:

firebase.auth().createUserWithEmailAndPassword(email, password)
    .catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  if (errorCode == 'auth/weak-password') {
    alert('The password is too weak.');
  } else {
    alert(errorMessage);
  }
  console.log(error);
});

根据文档,如果收到以下错误代码,则使用 error.emailerror.credential:

auth/credential-already-in-use
auth/account-exists-with-different-credential

https://firebase.google.com/docs/reference/js/firebase.auth.Auth#error-codes_5

https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#sign-inwith-credential

关于javascript - 尝试在 firebase 中链接多个身份验证提供程序时,从 error.email 获取 "undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59804504/

相关文章:

javascript - 将复选框添加到表中

reactjs - 错误 : Objects are not valid as a React child (found: object with keys {}). 如果您打算呈现子集合,请改用数组

swift - 根据已知的子值获取 Firebase 节点的父值?

firebase - "firebase serve"只是一个计划 Vanilla 开发服务器吗?

javascript - 图像选择器返回 uri `content://media/`,没有 React Native 中的扩展名

javascript - 停止在 hashchange 上触发 popstate

javascript - 当 page.html 位于另一个文件夹中时,幻灯片菜单停止工作

JavaScript 替换字符串中的所有逗号

node.js - 如何将静态 YAML 文件与 React Native 应用程序捆绑在一起?

react-native - 位于 "node_modules/web3-eth-accounts/src/index.js"的包试图导入 Node 标准库模块 "crypto"