Play Framework How to update a the session within a request

Viewed 1127

I'm following the documentation of the play framework and action composition. My action checks the current session to see if a user is present if so a UserRequest is returned and if not I would like to create a guest user, store them in the session of the request and return a UserRequest with the updated session.

class UserRequest[A](val email: String, request: Request[A]) extends WrappedRequest[A](request)

class UserAction @Inject()(val parser: BodyParsers.Default, dbConfigProvider: DatabaseConfigProvider)(implicit val executionContext: ExecutionContext)
  extends ActionBuilder[UserRequest, AnyContent] with ActionTransformer[Request, UserRequest] {

  val userRepo = new UserRepository(dbConfigProvider)(executionContext)

  def transform[A](request: Request[A]) = {
    request.session.get("user") match {
      case None        => createGuestUser(SecureRandomUtil.generateSecureRandom(24)).map { u => 
        new UserRequest(u.email, request.withSession("user"->u.email))
      }
      case Some(email) => Future.successful(new UserRequest(email,  request))
    }
  }

However withSession is not a defined method on the request object. Is there a way to modify the session on the request object?

1 Answers
Related