package controllers;

import models.User;

import static play.data.Form.*;

import play.Logger;
import play.data.Form;
import play.mvc.*;

import views.html.*;

/**
 * Created by philipp on 31.08.14.
 */

public class Application extends Controller {
    final static Form<User> USER_FORM = form(User.class);

    public static Result index() {
        String sessionId = session().get("sessionId");
        User user = User.FINDER.where().eq("sessionId", sessionId).findUnique();
        Logger.info("sessionId: " + sessionId);
        if (sessionId == null || user == null) {
            Logger.info("not logged in!");
            return ok(login.render("Please Login!", USER_FORM));
        } else {
            Logger.info("User logged in:\n" + user.toString());
        }
        return ok(index.render("Welcome to 2Fact Auth Examples!", user, USER_FORM));
    }

    public static Result auth() {
        Form<User> userForm = USER_FORM.bindFromRequest();
        if (userForm.hasErrors()) {
            return redirect(controllers.routes.Application.index());
        }
        User fUser = userForm.get();
        final User user = User.getOrCreate(fUser.username);
        if (user == null) {
            return notFound();
        }
        if (user.verify(fUser.password)) {
            user.updateSession();
            session().put("sessionId", user.sessionId);
            user.save();
        } else {
            Logger.info("password invalid!");
        }
        return redirect(controllers.routes.Application.index());
    }

    public static Result logout() {
        String sessionId = session().get("sessionId");
        User user = User.FINDER.where().eq("sessionId", sessionId).findUnique();
        if (sessionId != null && user != null) {
            user.clearSession();
            user.save();
        }
        return redirect(controllers.routes.Application.index());
    }

}

@@ -385,32 +385,195 @@
 t(%22sessionId%22);%0A
+        if (sessionId == null) %7B%0A            Logger.info(%22not logged in!%22);%0A            return ok(login.render(%22Please Login!%22, USER_FORM));            %0A        %7D%0A
         User use
@@ -629,32 +629,32 @@
 ).findUnique();%0A
-
         Logger.i
@@ -677,32 +677,32 @@
 %22 + sessionId);%0A
+
         if (sess
@@ -701,29 +701,8 @@
 if (
-sessionId == null %7C%7C 
 user

Update Application.java