0

I am trying to create a basic web login/authentication system in Play. Based on Play docs Im supposed to persist data across requests through Play's Session cookie; this is what my login looks like:

  def login() = Action.async(parse.json) { implicit request =>
    implicit val loginInfoReads: Reads[LoginInfo] = Json.reads[LoginInfo]
    val newSession = request.session +
      ("test" -> "yep")
    // @todo: add real error handling
    val unauthedUser = request.body.validate(loginInfoReads)
      .getOrElse(throw new Exception("Something went wrong with the login request"))
    UserService.authAndGetUser(unauthedUser.email, unauthedUser.password).map { res =>
      Ok(res.name).withSession(newSession)
}

I can see the cookie included in the response cookies in Chrome dev tools, but when I make a subsequent request to get the data inside the session, I get an empty map:

Logger.debug(request.session.data.toString)

logs:

Map()

and attempting to access "test" via request.session.get("test") fails.

What am I missing here? Why is my session data not persisting across requests?

Thanks

jag
  • 193
  • 2
  • 4
  • 13

1 Answers1

0

Turned out it wasn't a Scala/Play problem, more general problem with Chrome and cookies when hitting localhost. Here's what fixed it for me:

https://stackoverflow.com/a/39233628/747340

Community
  • 1
  • 1
jag
  • 193
  • 2
  • 4
  • 13