2017-02-03 3 views
2

Ich habe das folgende Tutorial [1] zum Einrichten der Authentifizierung für meine Anwendung folgen. Jetzt muss ich die Authentifizierung ändern, indem ich das Session-Timeout für das Frontend Angular 2 hinzufüge. Das ist in 20 Minuten nach Ablauf der Sitzung und bitten Sie den Benutzer erneut, sich anzumelden.Hinzufügen von Session-Timeout für Angular 2 Authentifizierung Front-End

Wie könnte ich diese erweiterte Funktion für mein Authentifizierungssystem entwickeln?

[1] http://jasonwatmore.com/post/2016/09/29/angular-2-user-registration-and-login-example-tutorial

Antwort

1
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; 
+0

Können Sie bitte Ihre Antwort erklären.? Was hast du hier erwähnt .. ?? –

Verwandte Themen