19

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

adjuremods
  • 2,938
  • 2
  • 12
  • 17

9 Answers9

24

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();
}
});
yehyatt
  • 2,295
  • 3
  • 28
  • 31
6

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

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
wish kalkani
  • 106
  • 2
  • 2
2

Change isEmailVerified() to emailVerified;

MMG
  • 3,226
  • 5
  • 16
  • 43
1

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

Vitor Hugo Schwaab
  • 1,545
  • 20
  • 31
1

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}

Dharman
  • 30,962
  • 25
  • 85
  • 135
Oscar Junius
  • 320
  • 3
  • 10
0

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

AndiM
  • 2,196
  • 2
  • 21
  • 38
  • 2
    That's not necessary. If the new account was created, the user is also signed in. As @yehyatt said, you just need to reload the current user. – Rupam Das Feb 18 '18 at 07:10
0

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();
VadimKo
  • 210
  • 1
  • 5
0
  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

0

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
    }
}
Devin Pitcher
  • 2,562
  • 1
  • 18
  • 11