Stay signed in (or remember me)

347 views
Skip to first unread message

Dorinel

unread,
Oct 11, 2010, 2:10:07 PM10/11/10
to play-framework
What is the best way to implement in play framework the stay signed in
(or remember me) functionality? (The functionality when the user don't
have to log in again when he comes back to the site)

Erwan Loisant

unread,
Oct 11, 2010, 2:50:39 PM10/11/10
to play-fr...@googlegroups.com
Hi,

You can store the userid (or any string that lets you identify the
user) in the session.
In a controller:
session.put("userid", theid);
session.get("userid");

This is stored in a session cookie (signed so people can't mess with
it) so it expires at the end of the browser session. You can change
that by setting application.session.maxAge in application.conf
Ex:
application.session.maxAge=7d # remember for one week

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

--
Erwan Loisant

Dorinel

unread,
Oct 11, 2010, 3:26:22 PM10/11/10
to play-framework
Are you sure about that? (I mean maxAge of the session) In other
thread I've read that session disappears when browser is closed. May
be somewhere it is possible to change the type of session (like the
type cookie used)?


On Oct 11, 9:50 pm, Erwan Loisant <elois...@gmail.com> wrote:
> Hi,
>
> You can store the userid (or any string that lets you identify the
> user) in the session.
> In a controller:
> session.put("userid", theid);
> session.get("userid");
>
> This is stored in a session cookie (signed so people can't mess with
> it) so it expires at the end of the browser session. You can change
> that by setting application.session.maxAge in application.conf
> Ex:
> application.session.maxAge=7d # remember for one week
>

Julien Tournay

unread,
Oct 11, 2010, 3:34:32 PM10/11/10
to play-fr...@googlegroups.com
Indeed, the session expire when you close your browser...
Unless maxAge is set.

jto
Real Programmers don't need comments-- the code is obvious.

Lev Shock

unread,
Oct 11, 2010, 3:51:37 PM10/11/10
to play-fr...@googlegroups.com
But "remember me" functionality requires changing this setting for each user dynamically, isn't it?

Dorinel

unread,
Oct 11, 2010, 5:04:30 PM10/11/10
to play-framework
Yes, also it will be great to correctly integrate with secure module.

On Oct 11, 10:51 pm, Lev Shock <d...@hackerdom.ru> wrote:
> But "remember me" functionality requires changing this setting for each user
> dynamically, isn't it?
>
>
>
> On Tue, Oct 12, 2010 at 1:34 AM, Julien Tournay <boudhe...@gmail.com> wrote:
> > Indeed, the session expire when you close your browser...
> > Unless maxAge is set.
>
> > jto
>
> >> play-framewor...@googlegroups.com<play-framework%2Bunsubscribe@go­oglegroups.com>
> >> .
> >> > > For more options, visit this group athttp://
> >> groups.google.com/group/play-framework?hl=en.
>
> >> > --
> >> > Erwan Loisant
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "play-framework" group.
> >> To post to this group, send email to play-fr...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> play-framewor...@googlegroups.com<play-framework%2Bunsubscribe@go­oglegroups.com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/play-framework?hl=en.
>
> > --
> > Real Programmers don't need comments-- the code is obvious.
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "play-framework" group.
> > To post to this group, send email to play-fr...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > play-framewor...@googlegroups.com<play-framework%2Bunsubscribe@go­oglegroups.com>
> > .

Guillaume Bort

unread,
Oct 12, 2010, 4:57:44 AM10/12/10
to play-fr...@googlegroups.com
It is integrated with secure module:

// Remember if needed
if(remember) {
response.setCookie("rememberme", Crypto.sign(username) + "-" +
username, "30d");
}

It stores a signed cookie with the username that is valid 30 days.

And to retrieve it:

Http.Cookie remember = request.cookies.get("rememberme");
if(remember != null && remember.value.indexOf("-") > 0) {
String sign = remember.value.substring(0, remember.value.indexOf("-"));
String username = remember.value.substring(remember.value.indexOf("-") + 1);
if(Crypto.sign(username).equals(sign)) {
session.put("username", username);
index();
}
}

> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.


> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

--
Guillaume Bort, http://guillaume.bort.fr

For anything work-related, use g...@zenexity.fr; for everything else,
write guillau...@gmail.com

Reply all
Reply to author
Forward
0 new messages