/*
* Login validation
*/
Validator.validateLogin(username, password);
MySQLConnection conn = new MySQLConnection();
conn.connect();
username = "'" + username + "'";
ResultSet rs = conn.SQLSelect("SELECT * FROM users WHERE username = "
+ username + " LIMIT 1");
if (rs.next()) {
String passwordHash = rs.getString("password");
if (BCrypt.checkpw(password, passwordHash)) {
String id = String.valueOf(rs.getInt("id"));
String name = rs.getString("name");
String email = rs.getString("email");
String cell = rs.getString("cell");
if (rs.wasNull())
cell = null;
String creationDate = rs.getDate("creation_date").toString();
conn.disconnect();
HttpSession userSession = getThreadLocalRequest().getSession();
userSession.setAttribute("id", id);
userSession.setAttribute("name", name);
userSession.setAttribute("email", email);
userSession.setAttribute("cell", cell);
userSession.setAttribute("creationDate", creationDate);
System.out.println("User session created.");
return null;
} else {
conn.disconnect();
throw new BadLoginException(username, password,
"Incorrect password");
}
}
// Username does not exist
else {
conn.disconnect();
throw new BadLoginException(username, password,
"Incorrect username");
}
}