Auto logout with Angularjs based on idle user

前端 未结 10 683
面向向阳花
面向向阳花 2020-11-30 18:07

Is it possible to determine if a user is inactive and automatically log them out after say 10 minutes of inactivity using angularjs?

I was trying to avoid using jQue

10条回答
  •  春和景丽
    2020-11-30 18:38

    Played with Boo's approach, however don't like the fact that user got kicked off only once another digest is run, which means user stays logged in until he tries to do something within the page, and then immediatelly kicked off.

    I am trying to force the logoff using interval which checks every minute if last action time was more than 30 minutes ago. I hooked it on $routeChangeStart, but could be also hooked on $rootScope.$watch as in Boo's example.

    app.run(function($rootScope, $location, $interval) {
    
        var lastDigestRun = Date.now();
        var idleCheck = $interval(function() {
            var now = Date.now();            
            if (now - lastDigestRun > 30*60*1000) {
               // logout
            }
        }, 60*1000);
    
        $rootScope.$on('$routeChangeStart', function(evt) {
            lastDigestRun = Date.now();  
        });
    });
    

提交回复
热议问题