Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store cookie even if the session is closed

What would be the best approach for a Play! application to remember the user? I think the only possible solution is to use the client side cookies, right? But as soon as the browser shuts down, this session is destroyed and not valid for the next request? How did/do you solve(d) this?

As for now, I ser the crypted userid in the session (per session), like this:

session("userid", user.id);

And then I use the interceptor to avoid passing parameters every when I need them oft, like described here: How to avoid passing parameters everywhere in play2?

But how to remember the user, or even beter, automatically log the user in on the next request?

EDIT: 2016-03-11 Be aware that some browser may store the session cookie for a longer period. For instance you can set in Chrome to remember the open tabs on next visit. This means that the Play Session cookie will be restored next time you open the browser.

And as of Play 2.4 the session cookie maxAge (you need to set in the application.conf) is renamed to: play.http.session.maxAge

like image 559
adis Avatar asked Apr 18 '12 15:04

adis


People also ask

Which cookie remain even after the web browser is closed?

Persistent Cookies Basically, this type of cookie is saved on your computer so when you close it and start it up again, the cookie is still there.

Are session cookies deleted when browser is closed?

There are two types of cookies: session cookies and persistent cookies. Web browsers normally delete session cookies when the user closes the browser. Unlike other cookies, session cookies do not have an expiration date assigned to them, which is how the browser knows to treat them as session cookies.

Is cookie dependent on session?

Sessions are cookies dependent, whereas Cookies are not dependent on Session. The session ends when the user closes the browser or logout from the application, whereas Cookies expire at the set time. A session can store as much data as a user want, whereas Cookies have a limited size of 4KB.

Can a session cookie be blocked?

If the persistent cookie is blocked, then at least you can use the session cookie during the rest of the browser session. On the user's next visit to the site (after the session cookie has been deleted) you can restore their basket using the persistent cookie if it's available.


2 Answers

To make the session not time-out when a users closes their browser you can use the session.maxAge parameter in the application.conf.

e.g.:

# Set session maximum age in seconds (4w)
session.maxAge=2419200 
like image 90
LeeKemp Avatar answered Oct 23 '22 05:10

LeeKemp


Quoting from Play 2.0 Session Documentation:

There is no technical timeout for the Session. It expires when the user closes the web browser. If you need a functional timeout for a specific application, just store a timestamp into the user Session and use it however your application needs (e.g. for a maximum session duration, maxmimum inactivity duration, etc.).

For security reasons, modern browsers will invalidate cookies on exit, and this is not something you can change simply because it would allow hackers to bad things with credentials that they do not rightfully have.

I would reevalutate whether or not you truly want the user to stay logged in, since it is usually a security risk to do so. If, however, you decide that you still want the user to stay logged in, you will have to try something that is not cookie based, and at the moment, I'm not sure what that would look like.

like image 44
Sean Freitag Avatar answered Oct 23 '22 05:10

Sean Freitag