Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Email Verification Not Working Properly

Tags:

WHAT I HAVE

I am using Firebase Authentication in my app where the users can register using Email & Password. If the users have not verified their email, I disable some features until they verify their email.

I also have a button to explicitly trigger verification mail, which just calls, sendEmailVerification(). It works perfectly and verification mail is always sent.

THE PROBLEM

The user gets the verification mails, but when he/she verifies it and comes back to the app, the isEmailVerified() is always false. So my app still doesn't allow the user to use all functions in spite of the fact that he/she has verified their email.

But if they log out and login again, the isEmailVerified() returns true immediately. But is it not good to log out the user and login back again.

Is it a bug in Firebase? Or am I doing something wrong?

like image 288
Aritra Roy Avatar asked Dec 07 '16 17:12

Aritra Roy


People also ask

How do I verify my email on Firebase?

Enable Email Link sign-in for your Firebase projectOn the Sign in method tab, enable the Email/Password provider. Note that email/password sign-in must be enabled to use email link sign-in. In the same section, enable Email link (passwordless sign-in) sign-in method. Click Save.

How do I change my Firebase verification email?

To customize your Firebase project's email action handler, you must create and host a web page that uses the Firebase JavaScript SDK to verify the request's validity and complete the request. Then, you must customize your Firebase project's email templates to link to your custom action handler.

How do I use Firebase authentication email?

If you haven't yet connected your app to your Firebase project, do so from the Firebase console. Enable Email/Password sign-in: In the Firebase console, open the Auth section. On the Sign in method tab, enable the Email/password sign-in method and click Save.


2 Answers

Hey I know that this is a pretty old thread, but what solved the problem for me FIRAuth.auth()?.currentUser.reload(completion: { (error) in ... })

For anyone that is facing this problem. And I am using the latest Firebase version 10.0.1

UPDATE

Firebase has changed the names of their functions since I've posted my solution.

Please note that all of changes to the current user states can be observed by adding an stateDidChangeListener. Auth.auth()?.currentUser is not updated immediately, so what I did was save the current user to a global variable I created and maintained myself.

Auth.auth().addStateDidChangeListener { (auth, user) in  // ... } 

Whenever you call Auth.auth()?.currentUser.reload() a refreshed and ready to use user object will be passed through the stateDidChangeListener.

You can then check for any updated data(i.e emailVerified)

like image 133
Woody Jean-louis Avatar answered Oct 17 '22 00:10

Woody Jean-louis


Here's how I solved this using react-native:

const user = firebase.auth().currentUser; user.reload().then(() => {   console.log({emailVerified: user.emailVerified}) }) 

I coupled this with AppState to run the reload if user is bringing the app from the background to the foreground.

like image 27
user2887478 Avatar answered Oct 17 '22 00:10

user2887478