首页 文章

Authguard angularfire2检查电子邮件是否已通过验证

提问于
浏览
0

我目前在我的authguard中有以下代码,以防止未登录时访问路由,但我还希望将未经验证的用户发送到验证页面,我该怎么做?

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

1 回答

  • 1

    您可以检查 emailVerified 属性的值:

    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']);
        });
    }
    

    注意 . 您的代码中的 this.auth 可能已经是一个可观察的 . 无需将其包裹在 Observable.from() 中 .

相关问题