How do I implement auto logout after 30 minutes of idle time using the ng-idle
module of Angularjs?
Asked
Active
Viewed 4,839 times
1

Paul
- 26,170
- 12
- 85
- 119

user1457957
- 195
- 1
- 6
- 21
1 Answers
1
I really don't know much about angularjs, i'm doing a course on it but i'm just getting started. Though I do know someone on github who could help you. I've just included snippets here, but you should check out this site if you want to know more: https://github.com/HackedByChinese/ng-idle
Anyway, here it is:
Include angular-idle.js after angular.js. You can install using Bower with this command: bower install --save ng-idle.
Bare bones example:
// include the `ngIdle` module
var app = angular.module('demo', ['ngIdle']);
app
.controller('EventsCtrl', function($scope, Idle) {
$scope.events = [];
$scope.$on('IdleStart', function() {
// the user appears to have gone idle
});
$scope.$on('IdleWarn', function(e, countdown) {
// follows after the IdleStart event, but includes a countdown until the user is considered timed out
// the countdown arg is the number of seconds remaining until then.
// you can change the title or display a warning dialog from here.
// you can let them resume their session by calling Idle.watch()
});
$scope.$on('IdleTimeout', function() {
// the user has timed out (meaning idleDuration + timeout has passed without any activity)
// this is where you'd log them
});
$scope.$on('IdleEnd', function() {
// the user has come back from AFK and is doing stuff. if you are warning them, you can use this to hide the dialog
});
$scope.$on('Keepalive', function() {
// do something to keep the user's session alive
});
})
.config(function(IdleProvider, KeepaliveProvider) {
// configure Idle settings
IdleProvider.idle(5); // in seconds
IdleProvider.timeout(5); // in seconds
KeepaliveProvider.interval(2); // in seconds
})
.run(function(Idle){
// start watching when the app runs. also starts the Keepalive service by default.
Idle.watch();
});
Hope that helped :)

Doggy-B
- 47
- 10
-
Thanks for the answer! it is really helpful. One thing which is not working in my case is, After session expired, browser name is 'Your session has expired'. But after logging in also that name is not changing still it shows Your session has Expired. – user1457957 Sep 04 '15 at 06:47
-
ng-idle is working fine for first timeout. Once timed out, Then we log into application again after that it is not working. Any ideas? – user1457957 Sep 06 '15 at 04:22
-
Got it! Need to call Idle.watch() after we login to activate the idle again. – user1457957 Sep 06 '15 at 05:05
-
Hi [user1457957](http://stackoverflow.com/users/1457957/user1457957) could you please give me the detail code to implement ngIdle ? – DirtyMind Nov 24 '15 at 12:52