Advertisement
Guest User

Untitled

a guest
Feb 27th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. def authenticate = Action { implicit request =>
  2. signinForm.bindFromRequest.fold(
  3. formWithErrors => BadRequest(html.signin(formWithErrors)),
  4. user => Redirect(routes.Application.active).withSession(Security.username -> User.getUserName(user))
  5. )
  6. }
  7.  
  8. def authenticate(email: String, password: String) : Option[User] = {
  9. (findByEmail(email)).filter { (user => BCrypt.checkpw(password, user.password)) }
  10. }
  11.  
  12. def findByEmail(email: String) : Option[User] = {
  13. UserDAO.findOne(MongoDBObject("email" -> email))
  14. }
  15.  
  16. val signinForm = Form {
  17. mapping(
  18. "email" -> nonEmptyText,
  19. "password" -> text)(User.authenticate)(_.map(user => (user.email, "")))
  20. .verifying("Invalid email or password", result => result.isDefined)
  21. }
  22.  
  23. def authenticate = Action { implicit request =>
  24. signinForm.bindFromRequest.fold(
  25. formWithErrors => BadRequest(html.signin(formWithErrors)),
  26. user => Redirect(routes.Application.active).withSession(Security.username -> User.getUserName(user.get))
  27. )
  28. }
  29.  
  30. val optUser: Option[User] = ...
  31. val user: User = optUser.get
  32.  
  33. val optUser: Option[User] = ...
  34. optUser match {
  35. case Some(user) => // do something with user
  36. case None => // do something to handle the absent user
  37. }
  38.  
  39. val optUser: Option[User] = ...
  40. optUser.map(user => doSomething(user))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement