Advertisement
Guest User

Untitled

a guest
Apr 1st, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. Error Message
  2.  
  3. package models;
  4.  
  5. import com.avaje.ebean.Model;
  6.  
  7. import javax.persistence.Entity;
  8. import javax.persistence.Id;
  9.  
  10. @Entity
  11. public class User extends Model {
  12.  
  13. @Id
  14. public String email;
  15. public String name;
  16. public String password;
  17.  
  18. public User(String email, String name, String password) {
  19. this.email = email;
  20. this.name = name;
  21. this.password = password;
  22. }
  23.  
  24. public Finder<String,User> find = new Finder<String,User>(
  25. String.class, User.class
  26. );
  27.  
  28. public User authenticate(String email, String password) {
  29. return find.where().eq("email", email)
  30. .eq("password", password).findUnique();
  31. }
  32.  
  33.  
  34. }
  35.  
  36. package controllers;
  37. import models.User;
  38. import play.data.Form;
  39. import play.mvc.Controller;
  40. import play.mvc.Result;
  41. import views.html.index;
  42. import views.html.login;
  43.  
  44. public class Application extends Controller {
  45.  
  46.  
  47.  
  48. public Result index() {
  49.  
  50.  
  51. return ok(index.render("hello"));
  52. }
  53.  
  54. public Result login() {
  55.  
  56. return ok(login.render(form(Login.class)));
  57. }
  58.  
  59.  
  60.  
  61. public Result authenticate() {
  62. Form<User> loginForm = Form.form(User.class).bindFromRequest();
  63. if (loginForm.hasErrors()) {
  64. return badRequest(login.render(loginForm));
  65. } else {
  66. session().clear();
  67. session("email", loginForm.get().email);
  68. return redirect(
  69. routes.Application.index()
  70. );
  71. }
  72. }
  73.  
  74.  
  75. public class Login {
  76. public String email;
  77. public String password;
  78. public User user;
  79.  
  80. public String validate() {
  81. user.authenticate(email, password);
  82. if (user == null) {
  83. return "Invalid user or password";
  84. }
  85. return null;
  86. }
  87.  
  88.  
  89. }
  90. }
  91.  
  92. @(form: Form[Application.Login])
  93. <html>
  94. <head>
  95. <title>Zentasks</title>
  96. <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
  97. <link rel="stylesheet" type="text/css" media="screen" href="@routes.Assets.at("stylesheets/login.css")">
  98. </head>
  99. <body>
  100. <header>
  101. <a href="@routes.Application.index" id="logo"><span>Zen</span>tasks</a>
  102. </header>
  103.  
  104. @helper.form(routes.Application.authenticate) {
  105. <h1>Sign in</h1>
  106. @if(form.hasGlobalErrors) {
  107. <p class="error">
  108. @form.globalError.message
  109. </p>
  110. }
  111.  
  112. <p>
  113. <input type="email" name="email" placeholder="Email" value="@form("email").value">
  114. </p>
  115. <p>
  116. <input type="password" name="password" placeholder="Password">
  117. </p>
  118. <p>
  119. <button type="submit">Login</button>
  120. </p>
  121. }
  122.  
  123. </body>
  124. </html>
  125.  
  126. # Routes
  127. # This file defines all application routes (Higher priority routes first)
  128. # ~~~~
  129.  
  130. # Home page
  131. GET / controllers.Application.index()
  132. GET /login controllers.Application.login()
  133. POST /login controllers.Application.authenticate()
  134. # Map static resources from the /public folder to the /assets URL path
  135. GET /assets/*file controllers.Assets.at(path="/public", file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement