src/main/java/codeu/controller/LoginServlet.java
/**
   * This function fires when a user submits the login form. It gets the username and password from
   * the submitted form data, checks that they're valid, and either adds the user to the session so
   * we know the user is logged in or shows an error to the user.
   */
This function fires when a user submits the login form. It gets the username and password from
the submitted form data, checks that they're valid, and either adds the user to the session so
we know the user is logged in or shows an error to the user.
This function fires when a user submits the login form . It gets the username and password from the submitted form data checks that they ' re valid and either adds the user to the session so we know the user is logged in or shows an error to the user .
@Override
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    if (userStore.isUserRegistered(username)) {
      User user = userStore.getUser(username);
      if(BCrypt.checkpw(password, user.getPassword())) {
        request.getSession().setAttribute("user", username);
        Event event = new LoginLogoutEvent(username, "placeholderLink", Instant.now(), "login-event", true);
        eventStore.addEvent(event);
        response.sendRedirect("/conversations");
      }
      else {
        request.setAttribute("error", "Invalid password.");
        request.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(request, response);
      }
    }
    else {
      request.setAttribute("error", "That username was not found.");
      request.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(request, response);
    }
  }
@ Override public void doPost ( HttpServletRequest request , HttpServletResponse response ) throws IOException , ServletException { String username = request . getParameter ( "username" ) ; String password = request . getParameter ( "password" ) ; if ( userStore . isUserRegistered ( username ) ) { User user = userStore . getUser ( username ) ; if ( BCrypt . checkpw ( password , user . getPassword ( ) ) ) { request . getSession ( ) . setAttribute ( "user" , username ) ; Event event = new LoginLogoutEvent ( username , "placeholderLink" , Instant . now ( ) , "login-event" , true ) ; eventStore . addEvent ( event ) ; response . sendRedirect ( "/conversations" ) ; } else { request . setAttribute ( "error" , "Invalid password." ) ; request . getRequestDispatcher ( "/WEB-INF/view/login.jsp" ) . forward ( request , response ) ; } } else { request . setAttribute ( "error" , "That username was not found." ) ; request . getRequestDispatcher ( "/WEB-INF/view/login.jsp" ) . forward ( request , response ) ; } }
This function fires when a user submits the login form.
This function fires when a user submits the login form .
// if the password is correct, redirect user to conversations page // if the password isn't correct, redirect the user to login page again // if the username isn't registered within the UserStore, redirects the user to the login page