# Recipe to handle session expiration in ajax calls

**URL:** <https://www.ruby-forum.com/t/recipe-to-handle-session-expiration-in-ajax-calls/166386>\
**Category:** Rails\
**Created:** [April 24, 2009, 5:12pm UTC](https://www.ruby-forum.com/t/recipe-to-handle-session-expiration-in-ajax-calls/166386 "2009-04-24T17:12:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [April 24, 2009, 5:12pm UTC](https://www.ruby-forum.com/t/recipe-to-handle-session-expiration-in-ajax-calls/166386/1 "2009-04-24T17:12:20Z")

</div>

Hello,

I’m looking for a recipe to handle session logged out or expired in an  
ajax calls. For instance, lets say we have a form submission in ajax  
that the user needs to be authenticated to perform this action. I’d  
like to at least notice the user that he is not logged in anymore or  
even better prompt an authentication form.

Any suggestions?

RÃ©mi

---

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [April 24, 2009, 6:10pm UTC](https://www.ruby-forum.com/t/recipe-to-handle-session-expiration-in-ajax-calls/166386/2 "2009-04-24T18:10:15Z")

</div>

On Fri, Apr 24, 2009 at 5:12 PM, Rémi Gagnon  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Rémi

Just return the relevant information from your Ajax call

If you’re using RJS, you could do

render :update do |page|  
if authenticated?  
page.redirect\_to login\_path  
else  
page.alert(“You’re authenticated”)  
end  
end

Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [April 24, 2009, 8:31pm UTC](https://www.ruby-forum.com/t/recipe-to-handle-session-expiration-in-ajax-calls/166386/3 "2009-04-24T20:31:35Z")

</div>

Or do you want to periodically make an AJAX call to check if the user  
is still logged in? If that is the case you could use the Prototype  
PeriodicalExecuter method to call the server and verify that the user  
is still logged in. I assume you have some server side sweep that  
logs out old sessions. Combined with the periodic AJAX call, the user  
would be notified that they were logged out or even better give them  
an update that they are about to be logged out!

On Apr 24, 8:12 am, Rémi Gagnon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
