Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Error Message
- package models;
- import com.avaje.ebean.Model;
- import javax.persistence.Entity;
- import javax.persistence.Id;
- @Entity
- public class User extends Model {
- @Id
- public String email;
- public String name;
- public String password;
- public User(String email, String name, String password) {
- this.email = email;
- this.name = name;
- this.password = password;
- }
- public Finder<String,User> find = new Finder<String,User>(
- String.class, User.class
- );
- public User authenticate(String email, String password) {
- return find.where().eq("email", email)
- .eq("password", password).findUnique();
- }
- }
- package controllers;
- import models.User;
- import play.data.Form;
- import play.mvc.Controller;
- import play.mvc.Result;
- import views.html.index;
- import views.html.login;
- public class Application extends Controller {
- public Result index() {
- return ok(index.render("hello"));
- }
- public Result login() {
- return ok(login.render(form(Login.class)));
- }
- public Result authenticate() {
- Form<User> loginForm = Form.form(User.class).bindFromRequest();
- if (loginForm.hasErrors()) {
- return badRequest(login.render(loginForm));
- } else {
- session().clear();
- session("email", loginForm.get().email);
- return redirect(
- routes.Application.index()
- );
- }
- }
- public class Login {
- public String email;
- public String password;
- public User user;
- public String validate() {
- user.authenticate(email, password);
- if (user == null) {
- return "Invalid user or password";
- }
- return null;
- }
- }
- }
- @(form: Form[Application.Login])
- <html>
- <head>
- <title>Zentasks</title>
- <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
- <link rel="stylesheet" type="text/css" media="screen" href="@routes.Assets.at("stylesheets/login.css")">
- </head>
- <body>
- <header>
- <a href="@routes.Application.index" id="logo"><span>Zen</span>tasks</a>
- </header>
- @helper.form(routes.Application.authenticate) {
- <h1>Sign in</h1>
- @if(form.hasGlobalErrors) {
- <p class="error">
- @form.globalError.message
- </p>
- }
- <p>
- <input type="email" name="email" placeholder="Email" value="@form("email").value">
- </p>
- <p>
- <input type="password" name="password" placeholder="Password">
- </p>
- <p>
- <button type="submit">Login</button>
- </p>
- }
- </body>
- </html>
- # Routes
- # This file defines all application routes (Higher priority routes first)
- # ~~~~
- # Home page
- GET / controllers.Application.index()
- GET /login controllers.Application.login()
- POST /login controllers.Application.authenticate()
- # Map static resources from the /public folder to the /assets URL path
- GET /assets/*file controllers.Assets.at(path="/public", file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement