Search code examples
playframework-2.6silhouette

Play not recognising AuthenticatorResult as Result


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?

AuthenticatorResult is define here - http://api.play.silhouette.rocks/5.0.0/com/mohiva/play/silhouette/api/services/AuthenticatorResult.html

CookieAuthenticatorService is define here - http://api.play.silhouette.rocks/5.0.0/com/mohiva/play/silhouette/impl/authenticators/CookieAuthenticatorService.html


Solution

  • 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
    
                            })