Play not recognising AuthenticatorResult as Result


#1

In my code, I am returning the AuthenticatorResult created by the embed method of CookieAuthenticatorService. But I am getting compilation error

Error:(270, 27) type mismatch;
 found   : scala.concurrent.Future[com.mohiva.play.silhouette.api.services.AuthenticatorResult]
 required: play.api.mvc.Result
                          result

My code is

val result:Future[AuthenticatorResult] = silhouette.env.authenticatorService.embed(cookie, Ok(Json.toJson(JsonResultSuccess("found user"))))
result

the code works if I return Ok instead of result

This works

val result:Future[AuthenticatorResult] = silhouette.env.authenticatorService.embed(cookie, Ok(Json.toJson(JsonResultSuccess("found user"))))
/result
Ok(Json.toJson(JsonResultError("registration not complete")))

I have defined my Action as
def signInUser = silhouette.UserAwareAction.async {..}

What am I doing wrong?


#2

Are you sure that you have defined that action as async? If you return a result that is not wrapped in a future should also result in a compile error. And you say that this works for you. So my assumption is that you do not use an async action. This is also what your compile error says. It requires a Result but you return a Result wrapped in a Future.


#3

Oops, my bad. The issue is not with AuthenticatorResult but Future{AuthenticatorResult}. I should have used flatMap instead of map in my code just before returning result. This is the working piece of code.

val cookieAuthenticatorFuture: Future[CookieAuthenticator] = silhouette.env.authenticatorService.create(loginInfo) //create authenticator

                      cookieAuthenticatorFuture.flatMap(cookieAuthenticator => {
                        val cookieFuture: Future[Cookie] = silhouette.env.authenticatorService.init(cookieAuthenticator) //init authenticator
                        cookieFuture.flatMap(cookie => { //this was earlier map. Changed it to flatMap and it worked.
                          val result:Future[AuthenticatorResult] = silhouette.env.authenticatorService.embed(cookie, Ok(Json.toJson(JsonResultSuccess("found user"))))
                          result

                        })