Can I do async form validation in Play Framework 2.x (Scala)?

后端 未结 5 679
执笔经年
执笔经年 2021-02-01 03:14

I\'m making a real push to understand the async powers of Play but finding a lot of conflict with regard to places where async invocation fits and places where the framework see

5条回答
  •  走了就别回头了
    2021-02-01 03:50

    I've been struggling with this, too. Realistic applications are usually going to have some sort of user accounts and authentication. Instead of blocking the thread, an alternative would be to get the parameters out of the form and handle the authentication call in the controller method itself, something like this:

    def authenticate = Action { implicit request =>
      Async {
        val (username, password) = loginForm.bindFromRequest.get
        User.authenticate(username, password).map { user =>
          user match {
            case Some(u: User) => Redirect(routes.Application.index).withSession("username" -> username)
            case None => Redirect(routes.Application.login).withNewSession.flashing("Login Failed" -> "Invalid username or password.")
          }
        }
      }
    }
    

提交回复
热议问题