我正在尝试实施一个角度保护来检查用户是否最近已登录,以便他们可以更改密码,更新电子邮件等。
使用角度火焰2或角度我有一个用户的句柄(firebase.User)。但是,如何检查他们是否有资格“最近登录”以执行敏感操作?
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Observable } from 'rxjs/Rx';
import { Injectable, OnInit } from '@angular/core';
import { AuthService } from './auth.service';
import * as firebase from 'firebase/app';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {
}
canActivate(route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> {
return this.authService.user$.map((user) => {
if (user) {
// how to validate that this user is a recent login?
return true;
} else {
console.log('not authenticated');
this.router.navigateByUrl('/login');
return false;
}
}).first();
}
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.canActivate(route, state);
}
}
答案 0 :(得分:2)
为什么不在任何敏感信息之前始终重新进行身份验证。这是常见的做法,Firebase提供了用于重新身份验证的API。您可以查看Firebase ID令牌中的.card.with-scroll .card-body {
height: calc(100% - 44px);
overflow-y: auto;
-webkit-box-flex: 0;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
}
以查看上次登录的时间,但Firebase Auth不会记录最近登录的要求的条件,并且可能会因为保留而未获得保留出于安全原因改变它的权利。您最好只需要重新认证,或者您可以尝试更新密码/更新电子邮件,但如果您收到该特定错误,请重新进行身份验证,然后重试。