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

后端 未结 5 680
执笔经年
执笔经年 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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-01 03:49

    The same question was asked in the Play mailing list with Johan Andrén replying:

    I'd move the actual authentication out of the form validation and do it in your action instead and use the validation only for validation of required fields etc. Something like this:

    val loginForm = Form(
      tuple(
        "email" -> email,
        "password" -> text
      )
    )
    
    def authenticate = Action { implicit request =>
      loginForm.bindFromRequest.fold(
        formWithErrors => BadRequest(html.login(formWithErrors)),
        auth => Async {
          User.authenticate(auth._1, auth._2).map { maybeUser =>
            maybeUser.map(user => gotoLoginSucceeded(user.get.id))
            .getOrElse(... failed login page ...)
          }
        }
      )
    }
    

提交回复
热议问题