2017-02-23 82 views
0

我目前在我的authguard中有以下代码以防止未登录时访问路由,但我还想将未验证的用户发送到验证页面,我该怎么做?Authguard angularfire2检查电子邮件是否已验证

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

回答

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()内。

+0

我刚刚试过你在这里说的。我得到了: error_handler.js:56 EXCEPTION:未捕获(承诺):TypeError:无法读取属性'auth'null TypeError:无法读取null的属性'auth' –

+0

如果'auth'为null,则可能意味着您的当前用户未通过身份验证,因此您无法访问其属性。在尝试访问'auth.auth.emailVerified'之前,您可以通过在'auth'不为空的代码中测试来修复它。 – AngularChef

相关问题