我正在使用 Firebase Auth 为我的 React 应用开发身份验证系统。当用户注册时,firebase auth 会注册用户 (createUserWithEmailAndPassword) 并返回一个将 auth.uid 和 auth.emailVerified 设置为“false”的 promise 。这很好。
然后我使用 sendEmailVerification() 方法,以便可以验证电子邮件地址。我已经测试了代码,对于“有效”和“现有”电子邮件地址来说它可以正常工作。 “catch”确实会按预期给出重复电子邮件地址的错误。问题是,它不会发送不存在的电子邮件(我认为这是正确的行为),但它应该给出一个错误(这样我可以向用户显示),但它没有。
有人可以解释一下为什么我没有收到“不存在的电子邮件地址”错误吗?
export const unpSignUp = (newUser) => {
console.log("newUser", newUser);
return (dispatch, getState, { getFirebase, getFirestore }) => {
const firebase = getFirebase();
const firestore = getFirestore();
firebase.auth().createUserWithEmailAndPassword(
newUser.unp_a05_email,
newUser.unp_a10_password
)
.then((response) => {
return firestore
.collection("unps")
.doc(response.user.uid)
.set({
unp_a02_surname: newUser.unp_a02_surname,
unp_a03_name: newUser.unp_a03_name,
unp_a06_verify_email_sent_datetime: null,
unp_a07_email_verified_on_datetime: null,
unp_a18_acc_created_on_datetime: moment().format("YYYY MM DD HH:mm:ss"),
});
})
.then((response) => {
console.log("SIGNUP SUCCESS ", response);
// user has been signed up, next step is to send verification email
dispatch({ type: SIGNUP_SUCCESS })
})
.then(() => {
// user has been signed up, next step is to send verification email
console.log('send email adr verification')
return firebase.auth().currentUser.sendEmailVerification()
})
.then( (response) => {
console.log("Verification email sent", response);
const user = firebase.auth().currentUser
console.log('current user', user)
const unp = firestore.collection("unps").doc(user.uid);
return unp.update({
unp_a06_verify_email_sent_datetime: moment().format("YYYY MM DD HH:mm:ss"),
})
})
.then( () => {
console.log(`unp_a06_verify_email_sent_datetime update to ${moment().format("YYYY MM DD HH:mm:ss")} `)
})
.catch((error) => {
console.log("SIGNUP ERROR", error);
console.log("SIGNUP ERROR CODE", error.code);
console.log("SIGNUP ERROR MESAGE", error.message);
dispatch({ type: SIGNUP_ERROR, error });
});
};
};
最佳答案
sendEmailVerification
的返回类型是 Promise<void>
- 它不会告诉您在错误电子邮件或失败/退回邮件的情况下发送的任何错误代码。它提到的错误代码是关于 actionCodeSettings
If the actionCodeSettings is not specified, no URL is appended to the action URL. The state URL provided must belong to a domain that is whitelisted by the developer in the console. Otherwise an error will be thrown.
因此无法检查它是否是有效的电子邮件 ID。 (这是预期行为,因为接收邮件服务器有时可能会关闭,因此存在重试退回邮件的概念)
关于javascript - firebase sendEmailVerification 在不存在的电子邮件地址上不会给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62374782/