Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get notified in Firebase of email verification?

I want to use Firebase Auth for email/password sign up. The problem is that I only want to accept the user as authenticated when the email address used is also verified.

Normally we can use a listener to check when a user is signed in or no longer signed in:

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

The issue is that this listener is not triggered when the user clicks on the link in the verification email. It's only called once the account is created.

Doing the following in a loop works while waiting for the user to click the link:

        Auth.auth().currentUser!.reload() {
            if !Auth.auth().currentUser!.isEmailVerified {

but the problem is I ideally want to manage all the auth stuff in the listener callback, but the listener is not used when the email validation state changes.

Is there something I am missing?

I know on mobile you can use deep linking so the link in the mail would open the app again, but that's not what I am looking for as users could open the mail on their computer so the problem still needs to be solved.

For now the only solution I see is to send an nsnotification myself and handle it where I handle all the auth code, but it feels a bit messy.

like image 605
Joris Mans Avatar asked May 29 '18 14:05

Joris Mans


People also ask

Does Firebase charge for email verification?

The first 10K verifications for both instances (USA, Canada, and India and All other countries) are provided at no cost each month. You are only charged on usage past this no-cost allotment. Prices are per successful verification.

Can Firebase send OTP?

After adding his phone number, the user will click on the Get OTP button after that Firebase will send OTP on that number which is mentioned above.

How can I get my email id from Firebase?

To get the email address of the currently logged in user, use the getAuth function. For email and password / simplelogin you should be able to get the email like this: ref = new Firebase('https://YourFirebase.firebaseio.com'); email = ref. getAuth().


1 Answers

Here are some options:

  1. Set up a RTDB node to track the emailVerified of a user.
  2. Implement your own landing page for email verification: https://firebase.google.com/docs/auth/custom-email-handler
  3. In your landing page, when the user is verified, update emailVerified in that RTDB node.
  4. On the page where the user sent the email verification, listen to changes on that node. On change, call user.reload() to update emailVerified on the user and user.getIdToken(true) to make sure the token is udpated with the latest claims.

Another way is a variation of the above which is to add a continueUrl which will show a continue button on the default email verification to redirect to a page you own where you know the user is verified. You can also use a similar mechanism as above to pass the status change via real-time database on that page instead of the landing page.

like image 106
bojeil Avatar answered Sep 22 '22 17:09

bojeil