播放不将 AuthenticatorResult 识别为结果

Play not recognising AuthenticatorResult as Result

在我的代码中,我正在 returning 由 CookieAuthenticatorServiceembed 方法创建的 AuthenticatorResult。但是我遇到了编译错误

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

如果我 return 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

糟糕,我的错。问题不在于 AuthenticatorResult,而在于 Future{AuthenticatorResult}。在返回 result 之前,我应该在我的代码中使用 flatMap 而不是 map。这是工作代码。

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

                        })