Reputation: 537
I am trying user Authentication using firebase.
For Email and password based authentication, I want to check whether the email input by the user in the form really belongs to the user, I can do so by sending a verification link to the user.
So, after the form is submitted and all other validation are complete, i tried the following code:
firebase.auth().createUserWithEmailAndPassword(this.state.email,this.state.password)
.then(()=>{
var actionCodesettings={
url: 'https://localhost:3000/?email=' + firebase.auth().currentUser.email,
handleCodeInApp: false
}
firebase.auth.currentUser.sendEmailVerification(actionCodesettings)
.then(()=>{
console.log("Email Sent")
})
.catch((error)=>{
console.log("Unexpected error occured")
})
})
.catch((error)=>{
var errorCode = error.code;
if(errorCode === 'auth/email-already-in-use'){
this.setState({
Message:"Email already Used"
})
return;
}
else if(errorCode === 'auth/invalid-email'){
this.setState({
Message:"Email already Used"
})
return;
}
else if(errorCode === 'auth/weak-password'){
this.setState({
Message:"Password is too Weak"
})
return;
}
})
}
But I am not recieving any email,neither the message "Email Sent" is logged out.
Upvotes: 0
Views: 138
Reputation: 26171
You are getting a syntax error because you have used firebase.auth.currentUser
instead of firebase.auth().currentUser
.
The reason you don't see this error is because you are ignoring it by not handling error codes that you don't expect. A good way to pick up on these types of bugs is to use a switch
statement with a default
fallback case.
firebase.auth().createUserWithEmailAndPassword(this.state.email,this.state.password)
.then((result) => {
var actionCodesettings={
url: 'https://localhost:3000/?email=' + result.user.email,
handleCodeInApp: false
}
return result.user
.sendEmailVerification(actionCodesettings);
})
.then(() => {
this.setState({
Message:"Please check your email to verify it."
});
})
.catch((error)=>{
const errorCode = error.code;
switch (errorCode) {
case 'auth/email-already-in-use':
this.setState({
Message:"This email has already been used"
});
return;
case 'auth/invalid-email':
this.setState({
Message:"Email was invalid"
});
return;
case 'auth/weak-password':
this.setState({
Message:"Please use a more secure password"
});
return;
default:
console.error("Unexpected code while signing in and verifying user", error);
this.setState({
Message:"Unexpected error. Please contact support."
});
return;
}
});
Upvotes: 2