Guest User

Untitled

a guest
Nov 27th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. package controllers
  2.  
  3. import play.api._
  4. import play.api.mvc._
  5. import play.api.mvc.Results._
  6.  
  7. import play.api.data._
  8. import play.api.data.Forms._
  9.  
  10. import play.api.Play.current
  11.  
  12. import views._
  13.  
  14. case class Account(id: Int, username: String)
  15.  
  16. case class Login(username: String, password: String) {
  17. // Defines a login
  18. // A login is NOT an account
  19. def isValid = {
  20. username == "admin" && password == "pass"
  21. }
  22. def account = { Account(1, username) }
  23. }
  24.  
  25. trait AuthForms {
  26. implicit val loginForm = Form(
  27. tuple(
  28. "username" -> nonEmptyText,
  29. "password" -> nonEmptyText
  30. ) verifying ("Invalid email or password", result => result match {
  31. case (username, password) => Login(username, password).isValid
  32. })
  33. )
  34. }
  35.  
  36. object Auth extends Controller with AuthForms {
  37.  
  38. def login = Action { implicit request =>
  39. request.session.get(Security.username).map { user =>
  40. Unauthorized("You are already logged in")
  41. }.getOrElse {
  42. loginForm.bindFromRequest.fold(
  43. formWithErrors => Redirect(routes.Application.index()).flashing("error" -> "Login failed"),
  44. login => Redirect(routes.Application.index()).flashing("info" -> "You were now logged in").withSession(Security.username -> login._1)
  45. )
  46. }
  47. }
  48.  
  49. def logout = Action { request =>
  50. Redirect(routes.Application.index()).flashing("info" -> "You were logged out").withNewSession
  51. }
  52.  
  53. }
  54.  
  55. object Application extends Controller with AuthForms {
  56.  
  57. def index = Action { implicit request =>
  58. Ok(views.html.index())
  59. }
  60.  
  61. }
Add Comment
Please, Sign In to add comment