Play Framework: Session's Gotchas

I've just switched our cookies to Play's Session. I was previously under the impression that "Session" is temporary, as in the stored value isn't persisted after the browser is closed.

After looking closely at it, Play's Session is implemented using a cookie, and we can simply set maxAge in order to make it behave like a persisted cookie.

Here are the 2 gotchas:

  1. play.http.session.maxAge is of the type FiniteDuration. Therefore, in your application.conf, you would have to set it to something like play.http.session.maxAge="31536000s", not just 31536000.
  2. You can use addingToSession and removingFromSession as the replacements for withCookies and discardCookies.
    1. Please note that withSession isn't a replacement for withCookies because it replaces the whole Session. It doesn't add like how withCookies adds new cookies to the current set of cookies.

Subscribe to tanin

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe