我遵循以下tutorial1为我的应用程序设置身份验证。现在,我需要修改身份验证,为Angular 2前端添加会话超时。也就是说,在会话到期后的20分钟内,应要求用户再次登录。
如何为我的身份验证系统开发此扩展功能。
1
发布于 2017-08-22 05:28:35
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/filter';
import 'rxjs/add/Observable/timer';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/switch';
@Injectable()
export class AuthService {
private authState: AuthState;
private authManager: BehaviorSubject<AuthState>;
public authChange$: Observable<AuthState>;
constructor() {
this.authManager = new BehaviorSubject(AuthState.LOGGED_OUT);
this.authChange$ = this.authManager.asObservable();
this.authChange$
.filter((authState:AuthState) => authState === AuthState.LOGGED_IN)
.map( (authState:AuthState) => Observable.timer(SESSION_TIMEOUT))
.do( () =>
console.log('Logged In. Session Timout counting down from now'))
.switch()
.subscribe( () => {console.log('Timer ended: Logging out')
this.logout();
});
}
login() {
this.setAuthState(AuthState.LOGGED_IN);
}
logout() {
this.setAuthState(AuthState.LOGGED_OUT);
}
emitAuthState():void {
this.authManager.next(this.authState);
}
private setAuthState(newAuthState:AuthState):void {
console.log('AuthService: setAuthState: ',
AuthState[newAuthState.toString()]);
if (newAuthState != this.authState) {
this.authState = newAuthState;
this.emitAuthState();
}
}
export enum AuthState {
LOGGED_IN,
LOGGED_OUT
}
const SESSION_TIMEOUT = 5000;https://stackoverflow.com/questions/42020222
复制相似问题