Angular User Session Timeout example step by step
This functionality let the user know that their session is about to expire and that they
would be logged out if they didn’t take action. I have written
this functionality in Angular for my latest application.
If you are new to Angular 6 then read my article about Setting up Angular 6 step by step before jump to this article to get handy in Angular 6.
Dealing with timers in Angular can be significantly different when using React components and subscriptions. The basic premises remains the same, though. We’ll have a service with a timer that will provide a Subject to which consumers of the service can subscribe. A React Subject provides an easy mechanism to trigger a “next” subscription to alert consumers that the timer has expired.
If you are new to Angular 6 then read my article about Setting up Angular 6 step by step before jump to this article to get handy in Angular 6.
Dealing with timers in Angular can be significantly different when using React components and subscriptions. The basic premises remains the same, though. We’ll have a service with a timer that will provide a Subject to which consumers of the service can subscribe. A React Subject provides an easy mechanism to trigger a “next” subscription to alert consumers that the timer has expired.
import { Injectable } from '@angular/core';
import { Observable, Subject, Subscription, BehaviorSubject} from 'rxjs/Rx';
@Injectable()
export class IdleTimeoutService {
private _count: number = 0;
private _serviceId: string = 'idleTimeoutSvc-' + Math.floor(Math.random() * 10000);
private _timeoutSeconds: number = 5;
private timerSubscription: Subscription;
private timer: Observable<number>;
private resetOnTrigger: boolean = false;
public timeoutExpired: Subject<number> = new Subject<number>();
constructor() {
console.log("Constructed idleTimeoutService " + this._serviceId);
this.timeoutExpired.subscribe(n => {
console.log("timeoutExpired subject next.. " + n.toString())
});
this.startTimer();
}
|
public startTimer() {
if (this.timerSubscription) {
this.timerSubscription.unsubscribe();
}
this.timer = Observable.timer(this._timeoutSeconds * 1000);
this.timerSubscription = this.timer.subscribe(n => {
this.timerComplete(n);
});
}
public stopTimer() {
this.timerSubscription.unsubscribe();
}
public resetTimer() {
if (this.timerSubscription) {
this.timerSubscription.unsubscribe();
}
this.timer = Observable.timer(this._timeoutSeconds * 1000);
this.timerSubscription = this.timer.subscribe(n => {
this.timerComplete(n);
});
}
private timerComplete(n: number) {
this.timeoutExpired.next(++this._count);
if (this.resetOnTrigger) {
this.startTimer();
}
}
}
ngOnInit() {
this.startCounter();
this._idleTimerSubscription = this.idleTimeoutSvc.timeoutExpired.subscribe(res => {
var modalPromise = this.dialogSvc.open("Session Expiring!", "Your session is about to expire. Do you need more time?",
true, "Yes", "No");
var newObservable = Observable.fromPromise(modalPromise);
newObservable.subscribe(
(res) => {
if (res === true) {
console.log("Extending session...");
this._status = "Session was extended.";
this.idleTimeoutSvc.resetTimer();
this.startCounter();
this.changeRef.markForCheck();
} else {
console.log("Not extending session...");
this._status = "Session was not extended.";
this.changeRef.markForCheck();
}
},
(reason) => {
console.log("Dismissed " + reason);
this._status = "Session was not extended.";
this.changeRef.markForCheck();
}
);
});
}
While I didn’t illustrate it, this becomes the starting point for hooking into Http interceptors to access a heartbeat API, or some other endpoint, to extend the user’s session at the server level (or cookie refresh). All in all, timers and observable s are pretty fun to play around with in Angular.
Thank you for reading. Keep visiting this blog and share this in your network. Please put your thoughts and feedback in the comments section.
Comments
Post a Comment