Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular session timeout and management [duplicate]

Is there any way to manage user session using Angularjs?, I mean::

  • Session timeout - when system is idle.
  • Alerts when session is near to expire with option to resume session.
  • Redirect (or any other action) when trying to make a request if session has expired.

Could be Interceptors one good option to solve this problem? Can you provide an example?

like image 519
Osy Avatar asked May 22 '13 16:05

Osy


People also ask

How to implement user Idle timeout in Angular?

Since AppComponent is the main/bootstrap component in the Angular application, we need to implement this logic in it. We are setting the Idle time 5 seconds which means we will get a warning message after 5 seconds of inactivity and timeout as 5 seconds which means it will be timed out within another 5 seconds.

What is session timeout?

Session timeout represents the event occuring when a user does not perform any action on a web site during an interval (defined by a web server). The event, on the server side, changes the status of the user session to 'invalid' (ie.


1 Answers

Try ng-idle. It's simple component where you can set the timeout and warning time before the timeout is reached. Then you can query server for user logout or something similar.

myApp.config(function(IdleProvider, KeepaliveProvider) {   IdleProvider.idle(900); // 15 min   IdleProvider.timeout(60);   KeepaliveProvider.interval(600); // heartbeat every 10 min   KeepaliveProvider.http('/api/heartbeat'); // URL that makes sure session is alive });  myApp.run(function($rootScope, Idle) {   Idle.watch();   $rootScope.$on('IdleStart', function() { /* Display modal warning or sth */ });   $rootScope.$on('IdleTimeout', function() { /* Logout user */ }); }); 

In the above configuration, when user is idle for 900s (does not move mouse, press any key or button etc), warning is being displayed. It will then wait 60s and log out user (send request to a server that possibly destroys server session).

In order to make sure server session does not expire (even if everything user is doing is moving mouse) the Keepalive service will send a request to the server every 10 minutes. This time has to less than server session expiration time.

Checkout the demo.

like image 134
fracz Avatar answered Sep 18 '22 06:09

fracz