Reputation: 614
My skills with firebase is basic, what i want to get is, only the users who have the verified mail will be able to enter the app, if not show the error of the email that has not been verified. here my code:
login (){
const user = firebase.auth().currentUser;
const emailVerified = user.emailVerified;
const validate = this.refs.formId.getValue();
if (validate && emailVerified != 'false') {
firebase.auth().signInWithEmailAndPassword(validate.email, validate.password)
.then(() => {
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
if (errorCode === 'auth/wrong-password') {
Toast.show({ text: 'Wrong password!', position: 'bottom', buttonText: 'Try Again' })
if (emailVerified === 'false') {
Toast.show({ text: 'Email Not Verified!', position: 'bottom', buttonText: 'Try Again' })
}else{
Toast.show({ text: 'Something Wrong!', position: 'bottom', buttonText: 'Try Again' })
}
});
}
}
I get this error: null is not an object (evaluating 'user.emailVerified)
Upvotes: 3
Views: 5058
Reputation: 2771
From notice under the example just like your way to utilize firebase.auth().currentUser
in documentation:
Note: currentUser might also be null because the auth object has not finished initializing. If you use an observer to keep track of the user's sign-in status, you don't need to handle this case.
@bdroid, you don't need to integrate them, by reforming the flow of this login process. I think this also provides a proper login flow, call signInWithEmailAndPassword
first, after detecting if the user is verified, decide what to do separately on complete authorization login and uncompleted one:
login (){
const validate = this.refs.formId.getValue();
firebase.auth().signInWithEmailAndPassword(validate.email, validate.password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode === 'auth/wrong-password') {
Toast.show({ text: 'Wrong password!', position: 'bottom', buttonText: 'Try Again' });
}
});
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
if (user.emailVerified === false) {
Toast.show({ text: 'Email Not Verified!', position: 'bottom', buttonText: 'Try Again' });
} else {
// successful login
}
} else {
// Toast.show({ text: 'Something Wrong!', position: 'bottom', buttonText: 'No user is signed in.' });
}
});
}
Upvotes: 1