Ruby on Railsを - Authlogic:ユーザーセッションが有効であるかどうかを定期的にチェック

StackOverflow https://stackoverflow.com/questions/2203777

質問

私は、ユーザーのセッションが期限切れので、ログインページに彼をリダイレクトする場合しているなら、私は定期的に確認できるようにするソリューションを探しています。
私はAuthlogicの宝石を使用して、それでは私がやっていることはCURRENT_USERのテストを行う関数を呼び出すですよ。
私USER_SESSION_TIMEOUTは、5分はので、私はこのAJAX呼び出しごとに5時10分を作るです。

<%= 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分に更新されていることに気づいた。
唯一のタブが開かれたが、私は2つのタブを持っている場合、私はcheck_session_timed_outのCURRENT_USERを呼び出すたびに、更新にユーザーを更新し、そのセッションが期限切れにならない問題はありません。

任意のアイデア? おかげ

役に立ちましたか?

解決

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

他のヒント

Authlogicはあなたのためにこれを行うことができます。ちょうどあなたのモデルで使用します:

Userモデルの場合:

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

...とUserSessionモデルでます:

self.logout_on_timeout = true

そして単に仕事! = D

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top