与Angularjs自动注销基于用户闲置 [英] Auto logout with Angularjs based on idle user

查看:333
本文介绍了与Angularjs自动注销基于用户闲置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能以确定如果用户处于非活动状态,并自动记录出来后说使用angularjs闲置超过10分钟?

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

我试图避免使用jQuery,但我无法找到如何做到这一点的angularjs任何教程或文章。任何帮助将是AP preciated。

I was trying to avoid using jQuery, but I cannot find any tutorials or articles on how to do this in angularjs. Any help would be appreciated.

推荐答案

我写了一个叫做模块伍空闲,可能会在这种情况下对你有用。 这里是一个包含说明和演示页。

I wrote a module called Ng-Idle that may be useful to you in this situation. Here is the page which contains instructions and a demo.

基本上,它有一个用于启动一个计时器用于能够由用户活动(事件,如点击,滚动,打字)被打乱的空闲时间的服务。您也可以手动通过调用服务的方法中断超时。如果超时不被中断,那么倒计时警告在那里你可以提醒他们要注销该用户。如果警告倒数到0后,他们不回应,事件被广播应用程序可以响应。你的情况,它可以发出请求杀死他们的会话,并重定向到登录页面。

Basically, it has a service that starts a timer for your idle duration that can be disrupted by user activity (events, such as clicking, scrolling, typing). You can also manually interrupt the timeout by calling a method on the service. If the timeout is not disrupted, then it counts down a warning where you could alert the user they are going to be logged out. If they do not respond after the warning countdown reaches 0, an event is broadcasted that your application can respond to. In your case, it could issue a request to kill their session and redirect to a login page.

此外,它有一个保持活动服务,可以在一个时间间隔执行ping一些网址。这可以通过你的应用程序中使用,以保持用户的会话活着,而他们是积极的。默认情况下,空闲服务,保持活动的服务整合,暂停执行ping,如果他们成为闲置状态,并恢复它,当他们回来了。

Additionally, it has a keep-alive service that can ping some URL at an interval. This can be used by your app to keep a user's session alive while they are active. The idle service by default integrates with the keep-alive service, suspending the pinging if they become idle, and resuming it when they return.

您只需要上手的信息是在网站与在的wiki 。然而,这里的配置中展示了如何给他们退出时,他们超时片段。

All the info you need to get started is on the site with more details in the wiki. However, here's a snippet of config showing how to sign them out when they time out.

angular.module('demo', ['ngIdle'])
// omitted for brevity
.config(function(IdleProvider, KeepaliveProvider) {
  IdleProvider.idle(10*60); // 10 minutes idle
  IdleProvider.timeout(30); // after 30 seconds idle, time the user out
  KeepaliveProvider.interval(5*60); // 5 minute keep-alive ping
})
.run(function($rootScope) {
    $rootScope.$on('IdleTimeout', function() {
        // end their session and redirect to login
    });
});

这篇关于与Angularjs自动注销基于用户闲置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆