/*
   * curl --data "[email protected]&password=test" http://localhost:8080/prostalytics/rest/auth/login
   *
   */
  @Path("/login")
  @POST
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  @Produces(MediaType.TEXT_HTML)
  public Response login(
      @FormParam("email") String email,
      @FormParam("password") String password,
      @HeaderParam("Auth-Token") String token)
      throws URISyntaxException {

    String newToken = null;
    try {
      User user = dao.authenticate(email, hashPassword(password));
      if (user != null) {
        newToken = auth.loggedIn(user, token);
      }

      URI redir =
          uriInfo
              .getBaseUri()
              .resolve(Navigation.fromLogin(user != null ? Navigation.OK : Navigation.ERROR));
      URI uri = UriBuilder.fromUri(redir).build();
      Response.ResponseBuilder res = Response.seeOther(uri);
      if (newToken != null) {
        res.header("Auth-Token", newToken);
      }
      return res.build();
    } catch (Throwable e) {
      URI uri =
          UriBuilder.fromUri(
                  uriInfo.getBaseUri().resolve(Navigation.fromLogin(Navigation.ERROR, e)))
              .build();
      return Response.seeOther(uri).build();
    }
  }
  /*
   * curl --data "name=Test&lastname=Tester&[email protected]&password=test" http://localhost:8080/prostalytics/rest/auth/register
   *
   */
  @Path("/register")
  @POST
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  @Produces(MediaType.TEXT_HTML)
  public Response register(
      @FormParam("email") String email,
      @FormParam("name") String name,
      @FormParam("lastname") String lastName,
      @FormParam("password") String password)
      throws URISyntaxException {

    try {
      User user = new User();
      user.setName(name);
      user.setLastname(lastName);
      user.setEmail(email);
      user.setPassword(hashPassword(password));
      validate(user);

      userDao.saveUser(user);

      URI uri =
          UriBuilder.fromUri(
                  uriInfo.getBaseUri().resolve(Navigation.fromAuthRegister(Navigation.OK)))
              .build();
      return Response.seeOther(uri).build();
    } catch (Throwable e) {
      e.printStackTrace(); // TODO :)
      URI uri =
          UriBuilder.fromUri(
                  uriInfo.getBaseUri().resolve(Navigation.fromAuthRegister(Navigation.ERROR, e)))
              .build();
      return Response.seeOther(uri).build();
    }
  }