Search code examples
scalaplayframeworkplayframework-2.2

Playframework Scala Authentication Builder Cached onUnauthorized


I'm trying to cached the Unauthorized result from the auth builder, but i can't find a way to properly request the result from cache (without using redirect with reverse route) any idea? this is my code:

  object Authenticated extends AuthenticatedBuilder(
    request =>
      request.session.get("email"),
    request =>
        Unauthorized(html.index("fail!", loginForm))
   )

I want to do something like:

  object Authenticated extends AuthenticatedBuilder(
    request =>
      request.session.get("email"),
    request =>
      Cached("Fail") { Action { implicit request =>
        Unauthorized(html.index("fail!", loginForm))
        }
      }
   )

this will of course return Cached instead of simpleResult and fail...

btw i'm using play 2.2.1


Solution

  • Thanks to @Peter i was able to return a SimpleResult with getOrElse, my final code is a bit more complex but here is an example that hopefully will help more people:

      import play.api.cache.Cache
      import play.api.Play.current
      import play.api.mvc.Security.AuthenticatedBuilder
    
      object Authenticated extends AuthenticatedBuilder(
        request =>
          request.session.get("email"),
        request =>
          Cache.getOrElse("UnAuth"){
            Unauthorized("Fail")
          }
       )