在AuthenticatedBuilder的异步版本中玩2

时间:2017-02-22 08:35:32

标签: scala asynchronous playframework-2.4

我正在尝试使用其他一些操作来构建基本身份验证:

  def findByNameSecure(username: String) = Authenticated { _ =>
    val cursor: Cursor[JsObject] = persons.
      find(Json.obj("userdetails.username" -> username)).
      cursor[JsObject](ReadPreference.primary)
    val res = cursor.collect[List]().map { persons =>
      Ok(Json.toJson(persons))
    }   .recover {
      case _ => BadRequest(Json.parse("{'error': 'failed to read from db'}"))
    }
    Await.result(res, 10.seconds)
  }

路线:

 GET      /secure/user/findbyname     controllers.UserController.findByNameSecure(username: String)

这可以按预期工作。令人不安的是,我使用了阻塞的Await.result。如何组成这种身份验证的异步版本?

我正在使用游戏2.4。

1 个答案:

答案 0 :(得分:3)

AuthendicatedBuilderActionBuilder的孩子。所以我认为它的async方法应该也可以。

使用示例:

def findByNameSecure(username: String) = Authenticated.async { _ =>