0

在我的代码中,我返回AuthenticatorResultembed. 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")))

我已将我的 Action 定义为 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

4

1 回答 1

0

哎呀,我的坏。问题不在于AuthenticatorResultbut Future{AuthenticatorResult}。我应该在返回之前在我的代码中使用flatMap而不是。这是工作代码。mapresult

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

                        })
于 2018-04-28T06:12:01.793 回答