Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to Rails session after :expire_after time is up?

Does the session become nil? Does the change take effect only on the next request?

I think I just asked three questions now...

like image 504
sargas Avatar asked May 01 '14 20:05

sargas


People also ask

What happens if a session is not timed out in rails?

If the session for that web application has not timed out, an attacker may execute unauthorized commands. In the session chapter you have learned that most Rails applications use cookie-based sessions. Either they store the session ID in the cookie and have a server-side session hash, or the entire session hash is on the client-side.

How to store user-specific state in rails?

This kind of user-specific state can be stored in the session. Rails provides a session object for each user that accesses the application. If the user already has an active session, Rails uses the existing session. Otherwise a new session is created. Read more about sessions and how to use them in Action Controller Overview Guide.

What is the difference between session and Cookie in rails?

By default, in Rails, there isn’t much of a difference. Rails does some work with the cookie to make it more secure. But besides that, it works the way you’d expect. Your Rails app puts some data into the cookie, the same data comes out of the cookie. If this was all there was, there’d be no reason to distinguish sessions from cookies.

What are sessions in Ruby on rails?

Rails provides a session object for each user that accesses the application. If the user already has an active session, Rails uses the existing session. Otherwise a new session is created. Read more about sessions and how to use them in Action Controller Overview Guide.


1 Answers

You can try to explore by using the similar settings:

AppName::Application.config.session_store :cookie_store, key: '_session_key', expire_after: 20.seconds

Then open up dev tools in your browser and go to cookies and select localhost cookies to see what happens.

I found out that:

  1. Session cookie gets deleted after the expiration time

  2. Expiration time for a cookie gets updated automatically (re-set) upon any request (even background ajax request counts)

  3. The effect by default will take place upon the next request (refreshing the page for example) and if you use typical authentication (has_secure_password_ for example) user should be logged out

I found the last comment on the ActionController::Base documentation page really helpful on this topic

like image 167
Nimir Avatar answered Sep 19 '22 11:09

Nimir