user6650650
user6650650

Reputation:

Firebase isEmailVerified returns false even after verifying email

I send email verification and click the link. Then I call isEmailVerified() on FirebaseUser object. This always return null, what may be the reason?

Upvotes: 19

Views: 10094

Answers (9)

Devin Pitcher
Devin Pitcher

Reputation: 2762

I use something like this. It returns the local value if the local value is true (since once verified it should never get unverified), else it refreshes the user and returns the updated value.

var isEmailVerified: Bool {
    get async {
        if Auth.auth().currentUser?.isEmailVerified == true {
            return true
        }
        
        try? await Auth.auth().currentUser?.reload()
        return Auth.auth().currentUser?.isEmailVerified == true
    }
}

Upvotes: 0

Oscar Junius
Oscar Junius

Reputation: 560

Same technique as @yehyatt´s solution but easier. Sign the user in with firebase, refresh the user and then check if the user is verified.

  1. Sign the user In

     Auth.auth().signIn(withEmail: YourEMail, password: YourPassword) { result, error in
         if error != nil {
             logInErrorReason = "\(error!.localizedDescription)"
             // An error ocurred
         }else {
             // Handle successful login.
         }
     }
    
  2. Refresh current user:

    Auth.auth().currentUser?.reload(completion: ((Error?) -> Void)?)

  3. Check if he is signed in:

    if Auth.auth().currentUser?.isEmailVerified == true { // Handle successful verification}

Upvotes: 1

Aung Phyo Zaw
Aung Phyo Zaw

Reputation: 17

  1. Create new user using createUserWithEmailAndPassword
  2. Let it send verification link to your gmail using sendEmailVerification
  3. go to your own gmail, you will see verification link and just click on that link
  4. try to login with username and email

You will see your own account had already verified and can proceed next setps

https://github.com/FirebaseExtended/flutterfire/issues/2208

Upvotes: 0

Ahmed Raafat
Ahmed Raafat

Reputation: 31

Change isEmailVerified() to emailVerified;

Upvotes: 3

wish kalkani
wish kalkani

Reputation: 106

First you have to call this method : signInWithEmailAndPassword , at least 1 time after user has verified their email ....

Only after that, you will get isEmailVerified() as true

Upvotes: 6

Re-authenticates the user who updates if the email has been verified ...

https://firebase.google.com/docs/auth/android/manage-users#re-authenticate_a_user

Upvotes: 1

VadimKo
VadimKo

Reputation: 210

isEmailVerified should provide correct response, however you might be using it incorrectly.

  1. If user verified his email ( he/she received an email from firebase, and clicked on "verify email" link ) then it will return true;
  2. If user didn't verified his email - it will return false

So solution for this issue is to update you registration process. When you create new user you should also issue a command to start email verification process await user.sendEmailVerification();

once your user will receive an email and will verify it. your method : user.isEmailVerified will return true

Your registration process should look something like that:

    AuthResult result = await _auth.createUserWithEmailAndPassword(
        email: email,password: password); 
    FirebaseUser user = result.user;
    await user.sendEmailVerification();

Upvotes: 0

yehyatt
yehyatt

Reputation: 2404

You should refresh the user object after verification as follows :

usertask = mAuth.getCurrentUser().reload();
usertask.addOnSuccessListener(new OnSuccessListener() {
@override
public void onSuccess(Void aVoid) {
user = mAuth.getCurrentUser();
boolean useremailveri = user.isEmailVerified();
String useremailuid = user.getUid();
}
});

Upvotes: 25

AndiM
AndiM

Reputation: 2188

I think you need to first login with user's Email and Password using signInWithEmailAndPassword then try calling isEmailVerified() method on returning user.

Upvotes: 0

Related Questions