user7342405
user7342405

Reputation:

Authguard angularfire2 check if email verified

I currently have the following code in my authguard to prevent routes from being visited when not logged in, but I would also like unverified users to be sent to a verify page, how would I do this?

canActivate(): Observable<boolean> {
return Observable.from(this.auth)
  .take(1)
  .map(state => !!state)
  .do(authenticated => {
    if (!authenticated) this.router.navigate(['/login']);
  })
}

Upvotes: 0

Views: 1907

Answers (1)

AngularChef
AngularChef

Reputation: 14077

You can check the value of the emailVerified property:

constructor(private af: AngularFire) { }

canActivate(): Observable<boolean> {
  return this.af.auth
    .take(1)
    .map(auth => auth.auth.emailVerified)
    .do(emailVerified => {
      if (!emailVerified) this.router.navigate(['/verify-email']);
    });
}

Note. this.auth in your code is probably already an observable. No need to wrap it inside Observable.from().

Upvotes: 2

Related Questions