@Route(method = HttpRequest.Method.POST, urlPattern = "/register")
  public HttpResponse register(
      HttpRequest request,
      @Param("name") String name,
      @Param("surname") String surname,
      @Param("email") String email,
      @Param("password") String password) {

    User user = new User(DB.getInstance().getNewId(), name, surname, email, password);
    DB.getInstance().addUser(user);

    HttpResponse response = new HttpResponse("Successfully created an user", 200);

    Cookie c = new Cookie("auth", request);
    response.addCookie(c);

    String sessionId = SessionManager.getSessionIdForRequest(request);
    SessionManager.getInstance().addSession(sessionId, new Integer(user.id));

    try {
      String body = TemplateProcessor.process("profile.html", user.getJsonData().build());
      response.setBody(body);
    } catch (IOException e) {
      response.setStatusCode(500);
    }

    return response;
  }
Esempio n. 2
0
  @Route(method = HttpRequest.Method.POST, urlPattern = "/register")
  public HttpResponse register(
      HttpRequest request,
      @Param("name") String name,
      @Param("password") String password,
      @Param("description") String description) {
    if (userExists(name.toLowerCase())) {
      return new HttpResponse("User already exists", 401);
    }
    addUser(new User(name, password, description));
    HttpResponse res = new HttpResponse("Successfully created an user", 200);
    Cookie c = new Cookie("auth", request);
    res.addCookie(c);

    String sessionId = SessionManager.getSessionIdForRequest(request);
    SessionManager.getInstance().addSession(sessionId, new UserSession(name));

    return res;
  }