6

ユーザーセッションの有効期限が切れているかどうかを定期的に確認し、期限切れの場合はログインページにリダイレクトできるソリューションを探しています。
私は Authlogic gem を使用しているので、現在のユーザーでテストを行う関数を呼び出しています。
私の USER_SESSION_TIMEOUT は 5 分なので、5:10 分ごとにこの ajax 呼び出しを行います。

<%= periodically_call_remote :url => {:controller => 'user_session', :action => 'check_session_timed_out'}, :frequency =>  (USER_SESSION_TIMEOUT + 10.seconds) %>

def check_session_timed_out
   if !current_user
      flash[:login_notice] = "Your session timed out due to a period of inactivity. Please sign in again."
      render :update do |page|
          page.redirect_to "/user_sessions/new"   
      end
   else
       render :nothing => true
   end
end

current_user を呼び出すたびに、ユーザー オブジェクトが更新されるため、セッションが 5 分に更新されることに気付きました。
1 つのタブしか開いていない場合は問題ありませんが、check_session_timed_out current_user renew を呼び出すたびに 2 つのタブがある場合、ユーザーを更新するため、セッションが期限切れになることはありません。

何か案が?ありがとう

4

2 に答える 2

6

Authlogic がこれを行います。モデルで使用するだけです:

ユーザー モデル:

acts_as_authentic do |c|
  c.logged_in_timeout(5.minutes)
end

...そして UserSession モデル:

self.logout_on_timeout = true

そして、ただ働くだけです!=D

于 2010-02-14T01:52:29.077 に答える
5

AuthLogicソース自体から:

# For example, what if you had a javascript function that polled the server
# updating how much time is left in their session before it times out. Obviously
# you would want to ignore this request, because then the user would never
# time out. So you can do something like this in your controller:

def last_request_update_allowed?
 action_name != "update_session_time_left"
end

あなたの場合、アクションの名前を使用してコントローラーにメソッドを追加する必要があります。

def last_request_update_allowed?
  action_name != "check_session_timed_out"
end
于 2010-02-05T00:33:26.993 に答える