Is there any way to manage user session using Angularjs?, I mean::
Could be Interceptors one good option to solve this problem? Can you provide an example?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With