当前在应用程序中,如果用户未通过身份验证,则他无法继续任何路线,但如果他/她已通过身份验证,则可以进行导航。这是通过 auth guard 中的 canActivate 函数实现的。如果我想在 5 分钟后用户空闲的情况下也有会话超时,那么通过 canActivate 函数和 Auth-Guard 实现它的方法是什么?
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (sessionStorage.getItem('currentUser')) {
// logged in so return true
return true;
}
// not logged in so redirect to login page with the return url
this.router.navigate(['/'], { queryParams: { returnUrl: state.url }});
return false;
}
}