Rails / Devise - 确定用户会话何时到期

时间:2013-05-22 00:54:19

标签: ruby-on-rails devise warden

当使用timeoutable模块进行设计时,如何确定当前用户会话到期之前的时间长度?目标是在所有响应中包含此值,以便客户端脚本可以使用它。我认为Devise在内部使用Warden进行身份验证,但我没有找到任何解释如何将会话到期时间拉出Warden深度的内容。

3 个答案:

答案 0 :(得分:10)

以下是您的操作方法:

class User < ActiveRecord::Base
  devise :authenticatable, :timeoutable, :validatable, :timeout_in => 20.minutes
end

答案 1 :(得分:7)

看起来你可以像Benjamin Tan说的那样把它放在模型中,或者你可以输入 config / initializers / devise.rb

# ==> Configuration for :timeoutable
# The time you want to timeout the user session without activity. After this
# time the user will be asked for credentials again. Default is 30 minutes.
# config.timeout_in = 30.minutes

请参阅此StackOverflow问题:Setting session length with Devise

答案 2 :(得分:1)

对于Rails 4:

<强>配置/初始化/ devise.rb

// please note the "controller as" syntax would be preferred, but that is out of the scope of this question/answer
<input ng-model="search" ng-change="fetchBeers()">

<强> user.rb

function SearchController($scope, $http) {

    $scope.search = "Sherlock Holmes";

    $scope.fetchBeers = function () {
        const query = `http://api.com/v2/search?q=${$scope.search}&key=[API KEY]&format=json`;
        $http.get(query).then(response => $scope.beers = response.data);
    };

}