【发布时间】:2015-09-11 15:57:16
【问题描述】:
我在 angularjs 中创建了一个网站。 现在我需要确保如果用户在登录超过 10 分钟后没有使用该网站,则应该进行自动注销。还应清除与网站关联的本地存储。 而且我还需要避免 URL 重写。 我是 angularjs 的新手。 我听说那届会议可以完成这些目标。 我需要一个好的建议以及获取参考的链接。
【问题讨论】:
-
你如何确定用户在登录后什么都不做?
我在 angularjs 中创建了一个网站。 现在我需要确保如果用户在登录超过 10 分钟后没有使用该网站,则应该进行自动注销。还应清除与网站关联的本地存储。 而且我还需要避免 URL 重写。 我是 angularjs 的新手。 我听说那届会议可以完成这些目标。 我需要一个好的建议以及获取参考的链接。
【问题讨论】:
【讨论】:
在.config运行后在app.js中声明这段代码,10分钟后它会重定向到登录页面
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
}
}
【讨论】:
您也可以使用angular-activity-monitor 来完成。
[ActivityMonitor] 是一个简单的服务,它会根据用户的 DOM 活动发出几个事件。只要用户被认为是“活跃的”,它还允许您在后台“让项目保持活跃”。
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");
});
}
【讨论】: