I handled session using Play framework in my application like below code.
def login = Action {
{ implicit request =>
val email = request.body.asFormUrlEncoded.get("email")(0)
val password = request.body.asFormUrlEncoded.get("password")(0)
loginForm.bindFromRequest.fold(
errors => BadRequest(html.login(errors,"Please enter valid username password")),
//contact => Ok(html.login(loginForm,SignUpProcess.login(email,password)))
contact => Redirect(routes.Application.home).withSession("email" -> email,"password" -> password)
)
}
}
def index = Action { request =>
request.session.get("email").map{ user =>
Redirect(routes.Application.home).withSession("email" -> user)
}.getOrElse{
Ok(views.html.login(loginForm,"Enter username password to login"))
}
}
I need to add timeout for my session. In Play documentation,
There is no technical timeout for the session, which 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.).
How to add timestamp into my user session and set maximum insctivity duration?
To Configure timeout in your Play application by setting values for configuration keys in conf/application.conf file.
Session time-out, i.e. the maximum age of the session cookie. If not set, the session expires when you close your web browser. For example, to set the session to one hour:
Remember the session for one week:
Default: the session is based on a transient cookie expires when the browser is closed.
or You can do something like :
During your login set a "last action time" to current time in the session.
In your Global class, override onRequest test if that flag exist
if not, user has no session -> redirect to login silently
if yes
--- test if last time is more than 30min ago
I did something like @arseniy-zhizhelev suggested
I had this same problem and added a timestamp (tick) to the session and updated it with each request after checking for a timeout.
Something like this:
http://www.poornerd.com/2014/04/01/how-to-implement-a-session-timeout-in-play-framework-2/
Then add a sessionTimeout=15 (in Minutes) to your conf file.
I would do it as follows. On every answer to the user I would write the current time to the session:
To ensure proper session time and email/password I would add an
Action
extension like this:The
Action
extension is used as follows:(you may have a look at https://github.com/Primetalk/todo-list/blob/master/app/controllers/SecuredController.scala and it's usage at https://github.com/Primetalk/todo-list/blob/master/app/controllers/Task.scala).
UPD:(regards to @kliron) The above
withSession
will replace the whole session. If you need to store there some other info, then you need to save it:and