Giter Club home page Giter Club logo

session-expiration-alert's Introduction

Session Expiration Alert

Build Status

npm

npm License

An Angular module to time session expiration. When user session idle time reaches a threshold, then pop up a modal dialog to let user choose to continue session or log out the system. When user session is expired, timer will stop and an alert dialog will ask for actions. A http interceptor is registered, so that session timer will restart at every http request.

v13.x Dependencies: Angular 13+.

v12.x Dependencies: Angular 12+, Bootstrap 4.5+ (css), NgBootstrap 10+.

v11.1.2 When user session is expired, timer will stop and an alert dialog will ask for actions.

v11.0.0 Breaking Change: Corrected the name for the SessionInterruptService service.

v11.x Dependencies: Angular 10+, Bootstrap 4.5+ (css), NgBootstrap 7+.

v10.x Dependencies: Angular 10+, Bootstrap 4.5+ (css), NgBootstrap 7+.

v9.x Dependencies: Angular 9+, Bootstrap 4+ (css), NgBootstrap 6+.

v6.x Dependencies: Angular 6+, Bootstrap 4+ (css), NgBootstrap 3+.

Usage

In app.module.ts

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    HttpClientModule,
    SessionExpirationAlertModule.forRoot(),
    // *** your other import modules
  ],
  providers: [
    {
      provide: SessionInterruptService,
      useClass: AppSessionInterruptService,
    },
  ],
  bootstrap: [AppComponent],
})
export class AppModule {}

In app.component.html

<session-expiration-alert></session-expiration-alert>

In app-session-interrupt.service.ts

@Injectable()
export class AppSessionInterruptService extends SessionInterruptService {
  constructor(private readonly httpClient: HttpClient) {
    super();
  }
  continueSession() {
    console.log(`I issue an API request to server.`);
  }
  stopSession() {
    console.log(`I logout.`);
  }
}

Then the SessionTimerService will start to count down at each second.

  • To provide actions in the alert modal dialog, you want to provide a AppSessionInterruptService class, which will be able to continue session via refreshing cookie, or stop session via logging out.

  • To start/stop/reset timer, inject SessionTimerService into your component or service, then call startTimer(), stopTimer(), resetTimer() as needed.

  • To get the remain time (in seconds), you can subscribe to remainSeconds$ in SessionTimerService.

Configuration

 imports: [
   // ***
    SessionExpirationAlertModule.forRoot({totalMinutes: 0.5}),
   //***
  ],

The SessionExpirationAlertModule accepts a configuration with interface of SessionExpirationConfig, which is an optional input and has a default value of total minutes = 20 min.

<session-expiration-alert
  [startTimer]="true"
  [alertAt]="2*60"
></session-expiration-alert>

[optional] startTimer indicates if the app needs to work on session expiration check or not. Default: true.

[optional] alertAt indicates when the alert modal dialog should show up. The value means how many seconds left till session becomes expired. Default: 60 (seconds).

session-expiration-alert's People

Contributors

changhuixu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

session-expiration-alert's Issues

Not working (paused) when a tab is inactive

Timer is not running in the background if totalTime is set above 7 minutes. Once the timer is started and open an another tab and come back after 7 minutes, timer is paused in between 5-6 minutes (approx.) and resumes once the tab is active again.

This is not ideal for application which has a server side idleTimeout runs without this issue.

Fix angular testability state

trackSessionTime prevents angular Testability stabilization and should be run outside of angular zone.

Here is proposed patch to fix the issue:

diff --git a/projects/session-expiration-alert/src/lib/components/session-expiration-alert/session-expiration-alert.component.ts b/projects/session-expiration-alert/src/lib/components/session-expiration-alert/session-expiration-alert.component.ts
index fd336f0..4552748 100644
--- a/projects/session-expiration-alert/src/lib/components/session-expiration-alert/session-expiration-alert.component.ts
+++ b/projects/session-expiration-alert/src/lib/components/session-expiration-alert/session-expiration-alert.component.ts
@@ -8,6 +8,7 @@ import {
   OnDestroy,
   OnInit,
   SimpleChanges,
+  NgZone
 } from '@angular/core';
 import { Subscription } from 'rxjs';
 import { SessionInterruptService } from '../../services/session-interrupt.service';
@@ -39,7 +40,8 @@ export class SessionExpirationAlertComponent
   constructor(
     private el: ElementRef,
     private sessionInterrupter: SessionInterruptService,
-    public sessionTimer: SessionTimerService
+    public sessionTimer: SessionTimerService,
+    private _ngZone: NgZone
   ) {}
 
   ngOnInit() {
@@ -60,6 +62,10 @@ export class SessionExpirationAlertComponent
   }
 
   private trackSessionTime() {
+    this._ngZone.runOutsideAngular(this.trackSessionTimeOutsideAngular)
+  }
+
+  private trackSessionTimeOutsideAngular() {
     this.sessionTimer.startTimer();
     this.expired = false;
     this.sessionTimerSubscription = this.sessionTimer.remainSeconds$.subscribe(

Injectable service in place of default Modal window service

This is not an issue with the tool but more like a feature request per se. Currently, a modal dialog box is displayed via a built-in service when the session is about to expire. While this is great default functionality and sufficient in most cases, it would be great if there was a way to allow another service to be injected that would get invoked instead; something very similar to how an implementation of SessionInteruptService can be injected.

All in all, great tool. Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.