0

I have made a website in angularjs. Now I need to make sure that if the user has not used the website after doing LOGIN for more than 10 mins automatic logout should be done. Also the local storage associated with the website should be cleared. And I also need to avoid URL rewriting. I am new to angularjs. I have heard that session can have to complete these goals. I need a good suggestion as well as links to get the reference.

Rathma
  • 1,196
  • 2
  • 33
  • 65
RAHUL DEEP
  • 695
  • 3
  • 13
  • 33
  • how you determine user is doing nothing after LOGIN ? – maddygoround Sep 11 '15 at 16:04
  • Possible duplicate of [Auto logout with Angularjs based on idle user](http://stackoverflow.com/questions/19168330/auto-logout-with-angularjs-based-on-idle-user) – Sean3z Jan 19 '16 at 14:02

3 Answers3

0

I would suggest to use this http://hackedbychinese.github.io/ng-idle/ Please check Auto logout with Angularjs based on idle user for more reference !

Community
  • 1
  • 1
user1608841
  • 2,455
  • 1
  • 27
  • 40
0

Declare this code in app.js after the .config has run,after 10 mins it will redirect to login page

var d = new Date();
var n = d.getTime();  //n in ms

$rootScope.idleEndTime = n+(10*60*1000); //set end time to 10 min from now
//console.log(idletime);
$document.find('body').on('mousemove keydown DOMMouseScroll mousewheel mousedown touchstart', checkAndResetIdle); //monitor events

function checkAndResetIdle() //user did something
{
    var d = new Date();
    var n = d.getTime();  //n in ms

    if (n>$rootScope.idleEndTime)
    {
         $document.find('body').off('mousemove keydown DOMMouseScroll mousewheel mousedown touchstart'); //un-monitor events

         //$location.search('IntendedURL',$location.absUrl()).path('/login'); //terminate by sending to login page

         alert('Session ended due to inactivity');
         $location.path("/login");
         //console.log($location.path());
                       $rootScope.$apply();
    }
    else
    {
         $rootScope.idleEndTime = n+(10*60*1000); //reset end time
    }
}
Hcorg
  • 11,598
  • 3
  • 31
  • 36
SiriK
  • 30
  • 2
0

You could also accomplish using angular-activity-monitor.

[ActivityMonitor] is a simple service that will emit a couple of events based on the users' DOM activity. It also allows you to "keep items alive" in the background so long as the user is considered "active".

angular.module('myModule', ['ActivityMonitor']);

MyController.$inject = ['ActivityMonitor'];
function MyController(ActivityMonitor) {
  // how long (in seconds) until user is considered inactive
  ActivityMonitor.options.inactive = 900; 

  // What to do once user is considered inactive
  ActivityMonitor.on('inactive', function() {
    alert("You're inactive");
  });
}
Sean3z
  • 3,745
  • 6
  • 26
  • 33