0

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

Session timeout - when system is idle. Alerts(popup with message do you want to continue (yes or no ) ) 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. and after session timeout user has to be loggedout automatically.

Thanks in advance.

1
  • 1
    Answering your question literally: Yes, there is a way to manage user session in Angularjs. If you are asking how to do this, that's a bit broad for this site. You might try starting out by experimenting with $timeout, and then ask a more focused question once you have an idea of if this will do what you want. Commented May 9, 2017 at 2:42

1 Answer 1

3

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.

Demo Link : http://hackedbychinese.github.io/ng-idle/

Sign up to request clarification or add additional context in comments.

2 Comments

thank u for your answer buddy. do u have any example with html . i want to show that. am very new to the angularjs
Please check demo link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.