无法阻止导航到下一页反应firebase
我正在使用Firebase创建一个Web应用程序,并进行反应。我的注册功能有效,但是我在边缘案例中遇到了一些麻烦,例如用户尝试使用已经存在的电子邮件注册时。如何防止导航到下一页?
async function handleSignup(e) {
e.preventDefault()
validatePassword()
try {
// setError('')
setLoading(true)
await signup(emailRef.current.value, passwordRef.current.value)
history.push('/profile')
console.log('created User' + currentUser.uid)
} catch {
setError('Failed to Create an Account')
}
setLoading(false)
}
function signup(email, password) {
auth.createUserWithEmailAndPassword(email, password)
.then((cred) => {
return db.collection('students').doc(cred.user.uid).set()
})
.catch((e) => {
if (e.code === 'auth/email-already-in-use') {
console.log('Email already in Use')
alert('Email already in use')
}
if (e.code === 'auth/weak-password')
alert(
'Password must be at least 7 characters long and contain special characters'
)
})
}
I am creating a web app using firebase and react. My signup function works but I am having some trouble with edge cases such as when a user tries to signup with an email that already exists. How do I prevent navigation to the next page?
async function handleSignup(e) {
e.preventDefault()
validatePassword()
try {
// setError('')
setLoading(true)
await signup(emailRef.current.value, passwordRef.current.value)
history.push('/profile')
console.log('created User' + currentUser.uid)
} catch {
setError('Failed to Create an Account')
}
setLoading(false)
}
function signup(email, password) {
auth.createUserWithEmailAndPassword(email, password)
.then((cred) => {
return db.collection('students').doc(cred.user.uid).set()
})
.catch((e) => {
if (e.code === 'auth/email-already-in-use') {
console.log('Email already in Use')
alert('Email already in use')
}
if (e.code === 'auth/weak-password')
alert(
'Password must be at least 7 characters long and contain special characters'
)
})
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要使用 Promise 。
You need to refactor your async
singup
using Promise.已经存在的电子邮件不是边缘案例:)
history.push('/profile')仅在注册为成功时才调用,
因此,如果注册失败,则应丢失错误并转到捕获块。
可能值得检查注册功能是否会如您期望的
An already existing email is not an edge case :)
history.push('/profile') should only be invoked if sign-up is succesfull,
so if signup fails it should throw an error and go to the catch block.
might be worth to check if signup function throws an error as you expect
一种方法是,当您在
signup
函数中遇到错误时,您应该抛出该错误,以便handleSignup
不会执行await 之后的代码注册(...)
。基本上,您的目标是在出现错误时突破“成功障碍”。
alert()
不会提供预期的行为,因为它不会抛出任何内容,这就是即使您的signup
也会触发history.push
的原因被拒绝并出现警报。相反,它会捕获错误并执行
setError(...)
。因此,您的
signup
函数将如下所示,One way of doing so is when you get an error in the
signup
function, you should throw the error sohandleSignup
won't execute the code that comes afterawait signup(...)
.Basically, your goal is to break out of the "success block" in case there's an error.
alert()
will not provide the expected behavior simply because it will not throw anything and that is the reasonhistory.push
triggers even though yoursignup
rejected and the alert showed up.Instead, it will catch the error and execute
setError(...)
.Therefore, your
signup
function would look something like this,Firebase 不允许您使用同一电子邮件注册两次。我建议使用他们的错误消息。
他们也不允许输入六个字符以下的密码。
您是否正在寻找适合其他系统的特定解决方案或通用解决方案?
您可以尝试我的存储库。我使用(免费)Firebase Auth 服务以及它们发回的错误。
Firebase is not allowing you to signup up twice with the same email. I recommend using their error messages.
They also do not allow entering a password under six characters.
Are you looking for a specific solution or a general solution that will fit other systems?.
You can try my repository. I worked with the (free) Firebase Auth service and the errors they send back.