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