简体   繁体   English

尝试在 firebase 中链接多个身份验证提供程序时,从 error.email 中获取“未定义”

[英]Getting "undefined" from error.email when trying to link multiple auth providers in firebase

I'm trying to link multiple auth providers to one account using firebase .我正在尝试使用firebase将多个身份验证提供程序链接到一个帐户。 The user is trying to create an account with the same address as the Google OAuth account which is already on firebase .用户正在尝试创建一个与 Google OAuth 帐户具有相同地址的帐户,该帐户已在firebase


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;
    }
})

The problem is I'm getting the proper error message:问题是我收到了正确的错误消息:

[Error: The email address is already in use by another account.] [错误:该电子邮件地址已被另一个帐户使用。]

and the proper error.code :和正确的error.code

auth/email-already-in-use身份验证/电子邮件已在使用

but, pendingCred or error.email come back undefined .但是, pendingCrederror.email返回undefined

Update更新
I took the advise and tried the following:我接受了建议并尝试了以下方法:

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

I'm getting the error:我收到错误:

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

You are using createuserwithEmailAndPassword which does not contain error.email or error.credential .您正在使用不包含error.emailerror.credential createuserwithEmailAndPassword According to the documentation to get the error you can either use error.message or error.code :根据获取错误的文档,您可以使用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);
});

According to the documentation, the error.email and error.credential is used if you get the following error code:根据文档,如果您收到以下错误代码,则使用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#error-codes_5

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JavaScript链接Firebase上的多个提供程序 - How to link multiple providers on firebase with javascript 尝试从 firebase 集合中提取数据时出错 REACT:类型错误:无法读取未定义的属性(读取“地图”) - Getting error when trying to pull data from firebase collection REACT: TypeError: Cannot read properties of undefined (reading 'map') ng-show =“ frm.Email。$ error.email”不显示无效的电子邮件错误 - ng-show=“frm.Email.$error.email ” doesn't show the error of invalid email 尝试解决承诺时,Firebase Auth给我一个错误 - Firebase Auth Is Giving Me An Error When Trying To Resolve A Promise 尝试使用Mailgun发送电子邮件时收到“ TypeError:未定义不是函数” - Getting “TypeError: undefined is not a function” when trying to send email using mailgun 在这里,我尝试使用 firebase 身份验证登录,但出现 400 错误。 我给出了准确的 email 和密码,但它显示如下错误 - Here, I am trying to login using firebase auth, but getting 400 error. I am giving exact email & password but it's showing below error 尝试上载到存储时出现firebase错误 - Getting firebase error when trying to upload to storage FirebaseError:Firebase:错误(身份验证/无效电子邮件) - FirebaseError: Firebase: Error (auth/invalid-email) 获取未捕获错误:Reference.set 失败:尝试将数据保存到 Firebase 存储时,第一个参数在属性中包含未定义 - Getting Uncaught Error: Reference.set failed: First argument contains undefined in property when trying to save the data to firebase storage 尝试使用JavaScript访问Firebase数据库时出现“未定义”错误 - 'undefined' error when trying to access Firebase DB using JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM