[英]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
.但是,
pendingCred
或error.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.email
或error.credential
createuserwithEmailAndPassword
。 According to the documentation to get the error you can either use error.message
or error.code
:根据获取错误的文档,您可以使用
error.message
或error.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.email
和error.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.