0

I need to verify that the email is confirmed as soon as the user confirms the email, because in the main screen of my application I open a dialog informing the user that he needs to confirm the email in order to use the application.

While it does not confirm the email the dialog is displayed and if it closes the dialog will open again.

I am validating as follows:

auth.service.ts:

export class AuthService {

 usuario: any; 

  constructor(private afAuth: AngularFireAuth) {}

      getUser() {
        return new Promise((resolve, reject) => {

          this.afAuth.auth.onAuthStateChanged((user: any) => {
            if (user) {
              this.user = user;
            }
            resolve(user);
          },
            (response: any) => {
              reject(response);
            });
        });
      }

main.component.ts:

    export class MainComponent {

    constructor( private authService: AuthService,){
            this.checkConfirmEmail();
    }

        checkConfirmEmail() {
            this.authService.getUser().then((user: any) => {
                if (user.emailVerified == false) {
                    this.showConfirmEmail();
                }
            });
        }
}

As above, it is only updated user.emailVerified when I refresh the page, I need a way so I do not have to refresh the page for user.emailVerified to come up to date.

Luiz Ricardo Cardoso
  • 1,554
  • 4
  • 16
  • 37
  • You'll need to either continuously send a request every few seconds to check if the email has been confirmed yet, or use server side events. – hostingutilities.com Mar 17 '18 at 04:56
  • I thought of using setTimeOut, but I do not find it a good practice ... I think there is a way using Observable something like this ... – Luiz Ricardo Cardoso Mar 17 '18 at 06:14
  • Yep. What you're wanting to do is called polling, and here is a SO answer about doing this using observables and Angular https://stackoverflow.com/questions/46728922/long-polling-in-angular-4 – hostingutilities.com Mar 17 '18 at 20:05

1 Answers1

0

A better real-time approach (HTTP polling is expensive and inefficient) is to build your own custom email verification landing page: https://firebase.google.com/docs/auth/custom-email-handler

Then on that landing page you set some flag in a Database node that only that user can access to confirm the email was verified when you apply the code. For more security, you can make that node readonly and send the code to your server (or some http endpoint hosted with cloud functions), verify it there and then use Admin SDK to write the status change to that user's node.

On the client device were the user is logged in (and has read access to that node), you listen to that database node changes to detect email verification.

bojeil
  • 29,642
  • 4
  • 69
  • 76