【问题标题】:Play not recognising AuthenticatorResult as Result播放未将 AuthenticatorResult 识别为结果
【发布时间】:2018-10-08 13:06:47
【问题描述】:

在我的代码中,我返回由embedCookieAuthenticatorService 方法创建的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

如果我返回 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

【问题讨论】:

    标签: playframework-2.6 silhouette


    【解决方案1】:

    糟糕,我的错。问题不在于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
    
                            })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      • 2023-03-24
      • 2019-07-17
      • 1970-01-01
      • 2022-01-26
      相关资源
      最近更新 更多