Login form validation with Async

227 views
Skip to first unread message

ovsyannikov vladimir

unread,
May 22, 2013, 3:15:00 AM5/22/13
to play-fr...@googlegroups.com
val loginForm = Form(
  tuple(
    "email" -> email,
    "password" -> text
  ) verifying("Invalid user name or password", fields => fields match { 
      case (e, p) => User.authenticate(e,p).isDefined 
  })
)

def authenticate = Action { implicit request => loginForm.bindFromRequest.fold( formWithErrors => BadRequest(html.login(formWithErrors)), user => gotoLoginSucceeded(user.get.id) ) }

How can I do the same in async way(for example
User.authenticate(e,p): Future[Option[User]])?
Do you planning add AsyncForm or
verifying/bindFromRequest overloads?

Johan Andren

unread,
May 22, 2013, 3:45:53 AM5/22/13
to play-fr...@googlegroups.com
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(formWith
Errors)),
    auth => 
      Async(
        User.authenticate(auth._1, auth._2).map { maybeUser =>
          maybeUser.map(user => gotoLoginSucceeded(user.get.id))
            .getOrElse( ... failed login page ...)
        }
      )
  )
}
Reply all
Reply to author
Forward
0 new messages