title: Scala Play 2: action composition and BodyParser
body: <p>I'm using action composition for authentication and to avoid passing common parameters in each action. My question is how can I combine it the BodyParser parse.json, like in the method below?</p>
  
  <pre><code>def setUser() = Action(parse.json) {
    implicit request =&gt;
      val id = (request.body \ "id").as[String]
      val username = (request.body \ "username").as[String]
      val previousURI = request.session.get("previousURI") match {
        case Some(x) =&gt; x
        case None =&gt; "/"
      }
      Ok(previousURI).withSession(
        "id" -&gt; id,
        "username" -&gt; username
      )
  }
  </code></pre>
  
  <p>The controller that uses the above method uses the 'Auth' trait:</p>
  
  <pre><code>case class User(id: Option[String], username: Option[String])
  
  case class Context(user: User, request: Request[AnyContent])
    extends WrappedRequest(request)
  
  trait Auth {
  
    def CheckLogin(f: Context =&gt; Result) = {
  
      Action { request =&gt;
        request.session.get("username").map { token =&gt;
          // username? yes continue...
          val id = request.session.get("id")
          val username = request.session.get("username")
          f(Context(new User(id, username), request))
        }.getOrElse {
          // no redirect to login
          Results.Redirect(routes.Application.login).withSession(
            "previousURI" -&gt; request.uri
          )
        }
  
      }
  
    }
  
  }
  </code></pre>
  
  <p>If I try:</p>
  
  <p>def myMethod() = CheckLogin(parse.json) { ...</p>
  
  <p>I get:</p>
  
  <pre><code>type mismatch; found : play.api.mvc.BodyParser[play.api.libs.json.JsValue] required: controllers.Context =&gt; play.api.mvc.Result
  </code></pre>
  
  <p>thanks!</p>