Pergunta

I'm new to scala, but have some experience using the play framework in Java. I've added the SecureSocial authentication library, which defines a SecuredACtion, and it seems to be working correctly. However, I'm having trouble understanding the expected content within the custom action in scala code.

Here's my controllers class. Ideally, "index" would simply redirect the authenticated request to "unprotectedIndex" somehow, but that doesn't seem to be possible. So if not, next best thing is simply to serve the file directly from inside of the secured action, but that's also not working.

What is missing from my code?

object Application extends Controller with securesocial.core.SecureSocial {
  // this doesn't compile, but it's a long scala exception that I don't know how to fix.
  def index = SecuredAction { implicit request =>
    Assets.at("/public", "index.html").apply(request) 
  }

  def unprotectedIndex = Assets.at("/public", "index.html")

}

It seems like it's expecting a SimpleResult but getting a Future[SimpleResult] - this feels like it shouldn't be complicated, but what am I missing?

Foi útil?

Solução

It seems like you are using play framework 2.2. There were some changes and most methods return Future[SimpleResult] instead of just Result or SimpleResult. You can check if you are able to do like this: def index = SecuredAction.async {...} (but I'm almost sure you can't).

You can use this approach to make it work correctly:

import scala.concurrent.Await
import scala.concurrent.duration._

def index = SecuredAction { implicit request =>
  Await.result(Assets.at("/public", "index.html").apply(request), 5 seconds) //you can specify you maximum wait time here
}

EDIT

Even one more thing to simplify:

Await.result(unprotectedIndex(request), 5 seconds)

So you can call your unprotectedIndex from your index Action

Outras dicas

So, just by looking at syntax highlighting in my IDE I have been able to get something that seems to compile and work but looks deeply wrong to me.

I changed it to this:

def index = SecuredAction { implicit request =>
    Assets.at("/public", "index.html").apply(request).value.get.get
  }

Is that the correct way to do this? It looks really weird to me, am I just not familiar with the idioms?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top