ripper234
ripper234

Reputation: 230028

How to know if a user is logged in to Play Framework?

I have a Play site that's part of a larger site that uses PHP. I would like to have the other site to behave different for users who logged in in Play (e.g., display a "logout" link instead of "login").

What is the correct way to determine, in a 3rd party server app, whether a user is logged in to Play or not? Should I just check for the existence of the PLAY_SESSION cookie?

I see that logging in creates this cookie, and logging out clears it, so it seems to be what I'm looking for.

Upvotes: 1

Views: 1784

Answers (2)

Pere Villega
Pere Villega

Reputation: 16439

It depends what you mean by logged in.

If you mean visiting the site (accessing the Play server) then that's the cookie you want. That cookie is created when you access the server and stores your session id amongst other data.

If you mean "using a username and password (or another authentication system) to access content on the site" then no. For that you'll probably be storing a user id in the session, which in turn will be a field in the Play cookie, and that field is what you would need to check for.

Upvotes: 2

Cyril Lacôte
Cyril Lacôte

Reputation: 98

I think the correct way is to test the existence of session["username"], with Groovy code like #if {session.username}in a template.

Upvotes: 0

Related Questions