package ru.qatools.school.twister.resources;

import org.glassfish.jersey.server.mvc.ErrorTemplate;
import org.glassfish.jersey.server.mvc.Template;
import ru.qatools.school.twister.models.Comment;
import ru.qatools.school.twister.models.Post;
import ru.qatools.school.twister.models.User;
import ru.qatools.school.twister.view.ViewData;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.SecurityContext;
import java.io.IOException;

/*
 * Created by ashchogoleva on 23.11.14.
 */
@Path("auth")
@Produces(MediaType.TEXT_HTML)
@ErrorTemplate(name = "/error.ftl")
public class AuthResource {

    @Context
    SecurityContext securityContext;

    @GET
    @Path("/register")
    @Template(name = "/auth/register.ftl")
    public ViewData showRegisterForm() {

        ViewData view = new ViewData();

        view.authUser = (User) securityContext.getUserPrincipal();

        return view;
    }

    @Context
    HttpServletRequest request;
    @Context
    HttpServletResponse response;

    @POST
    @Path("/register")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public String processRegister(@FormParam("name") String name,
                                  @FormParam("pass") String hash) throws IOException {

        User user = new User();
        user.setName(name);
        user.setPassHash(hash);
        user.saveIt();

        HttpSession session = request.getSession(true);
        session.setAttribute("userId", user.getId());

        response.sendRedirect("/user/" + user.getId());

        return "";
    }

    @GET
    @Path("/signin")
    @Template(name = "/auth/login.ftl")
    public ViewData showLoginForm() {

        ViewData view = new ViewData();

        view.authUser = (User) securityContext.getUserPrincipal();

        return view;

    }

    @POST
    @Path("/signin")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public String processLogin(@FormParam("name") String name,
                               @FormParam("pass") String hash) throws IOException {

        User user = User.findFirst("name = ? and pass_hash = ?", name, hash);

        if (user == null) {
            System.out.println("name or password is wrong");

            return "";
        }

        HttpSession session = request.getSession(true);
        session.setAttribute("userId", user.getId());

        response.sendRedirect("/user/" + user.getId());

        return "";
    }
}

@@ -2571,32 +2571,286 @@
 user.getId());%0A%0A
+        return %22%22;%0A    %7D%0A%0A    @GET%0A    @Path(%22/signout%22)%0A    public String processLogout() throws IOException %7B%0A%0A%0A        HttpSession session = request.getSession(true);%0A        session.setAttribute(%22userId%22, null);%0A%0A        response.sendRedirect(%22/%22);%0A%0A
         return %22

Add user logout. Fix #8