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

查看:20
本文介绍了使用基于空闲用户的 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 中执行此操作的教程或文章.任何帮助将不胜感激.

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.

推荐答案

我编写了一个名为 Ng-Idle 的模块,在这种情况下可能对您有用.这是包含说明和演示的页面.

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.

另外,它有一个keep-alive服务,可以每隔一段时间ping一些URL.您的应用程序可以使用它来在用户处于活动状态时保持其会话处于活动状态.idle 服务默认与 keep-alive 服务集成在一起,如果它们空闲就暂停 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.

开始使用所需的所有信息都在 网站 上,更多详细信息请参见维基.但是,这里有一个配置片段,展示了如何在它们超时时将它们注销.

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天全站免登陆