播放时无法将AuthenticatorResult识别为结果

时间:2018-04-27 16:21:19

标签: playframework-2.6 silhouette

在我的代码中,我将返回由AuthenticatorResult embed方法创建的CookieAuthenticatorService。但我收到编译错误

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

我的代码是

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

如果我返回Ok而不是result

,则代码有效

这有效

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

我已将我的行动定义为 def signInUser = silhouette.UserAwareAction.async {..}

我做错了什么?

AuthenticatorResult在这里定义 - http://api.play.silhouette.rocks/5.0.0/com/mohiva/play/silhouette/api/services/AuthenticatorResult.html

CookieAuthenticatorService在这里定义 - http://api.play.silhouette.rocks/5.0.0/com/mohiva/play/silhouette/impl/authenticators/CookieAuthenticatorService.html

1 个答案:

答案 0 :(得分:0)

哎呀,我的坏。问题不在于AuthenticatorResult,而在于Future{AuthenticatorResult}。在返回flatMap之前,我应该在代码中使用map而不是result。这是一段代码。

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

                        })