2010-02-04 3 views
5

사용자 세션이 만료되었는지 여부를 주기적으로 확인하고 로그인 페이지로 리디렉션 할 수있는 솔루션을 찾고 있습니다.
필자는 Authlogic gem을 사용하고 있습니다. 따라서 current_user에 대한 테스트를 수행하는 함수를 호출하고 있습니다.
내 USER_SESSION_TIMEOUT이 5 분이므로 매 5 분마다이 ajax를 호출합니다. Ruby on Rail - Authlogic : 주기적으로 사용자 세션이 유효한지 확인하십시오.


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 

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

은 내가 사용자 개체 CURRENT_USER 호출 할 때마다 업데이트되는 것으로 나타났습니다 그래서 세션은 5 분으로 갱신됩니다.
하나의 탭만 열려도 문제가되지 않지만 check_session_timed_out을 호출 할 때마다 2 개의 탭이있는 경우 current_user는 사용자를 갱신하므로 세션이 만료되지 않습니다.

어떤 아이디어가 있습니까? 감사

AuthLogic source itself에서

답변

5

: 귀하의 경우

# 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 
+0

MERCI! 저건 완벽 해 – Mathieu

6

Authlogic 할 수있는이 너를 위해서.

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

... 그리고 사용자 세션 모델 : 사용자 모델에

:

self.logout_on_timeout = true 

를 그리고 단순히 작동 그냥 모델에서 사용! = D