在我的代码中,我将返回由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
答案 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
})