我正在使用设计,自动注销效果很好。
但是,在用户发出另一个请求之前,不会通知用户他们已注销,此时他们将被重定向到登录页面。对于 AJAX 功能,这不是很好,它要么静默失败,要么引发异常。
设计维基似乎没有一个例子,有没有标准的解决方案?一个带有倒数计时器的 javascript 弹出窗口,如果用户没有点击“让我保持登录”,它会重定向吗?
我正在使用设计,自动注销效果很好。
但是,在用户发出另一个请求之前,不会通知用户他们已注销,此时他们将被重定向到登录页面。对于 AJAX 功能,这不是很好,它要么静默失败,要么引发异常。
设计维基似乎没有一个例子,有没有标准的解决方案?一个带有倒数计时器的 javascript 弹出窗口,如果用户没有点击“让我保持登录”,它会重定向吗?
我最终实现了类似于下面的 jscript 超时。
一些未回答的问题:
应用程序.js
//= require timer
// redirect user after 15 minutes of inactivity - should match Devise.timeout_in + 1 second grace period
$(function() {
var logout_timer = new Timer(901, 'users/sign_in', window);
logout_timer.start();
// restart timer if activity
$(document).on('keyup keypress blur change mousemove',function(){
logout_timer.start();
});
});
计时器.js
Timer = function(time_in_secs, path, windowobj) { // window object must be injected, else location replace will fail specs
var self = this; // 'this' not avail in setInterval, must set to local var avail to all functions
this.state = 'init'
this.time_remaining = time_in_secs;
this.timer_id = undefined;
this.start = function() {
// if restarting, there will be a timer id. Clear it to prevent creating a new timer, reset time remaining
if (this.timer_id !== undefined) {
this.time_remaining = time_in_secs;
this.clear_timer(this.timer_id, self);
}
this.state = 'running';
this.timer_id = setInterval(function() { // IE any version does not allow args to setInterval. Therefore, local variables or refer to self obj
self.time_remaining -= 1;
// log status every 10 seconds
if ((self.time_remaining % 10) === 0) {
console.log("logging user out in " + self.time_remaining + " seconds");
}
// when timer runs out, clear timer and redirect
if ( self.time_remaining <= 0 ) {
self.clear_timer(self.timer_id, self);
self.do_redirect(path, windowobj);
};
}, 1000);
return this.timer_id;
};
this.clear_timer = function(timer_id, self) {
self.state = 'stopped';
clearInterval(self.timer_id);
}
this.remaining = function() {
return this.time_remaining;
};
this.do_redirect = function(path, windowobj) {
console.log("Redirecting to " + path);
self.state = 'redirecting';
windowobj.location = path;
}
}