Added the /login and /register routes (part where I actually submit the database requests)

This commit is contained in:
TheNagaki 2023-02-14 23:21:26 +01:00
parent bbc752500a
commit 209b0eeeb4

View file

@ -110,4 +110,91 @@ public class DatabaseRepo {
}
return null;
}
/**
* Check if a pseudo is available
*
* @param pseudo The pseudo to check
* @return True if the pseudo is available, false if it's already taken
*/
public boolean checkPseudoAvailability(String pseudo) {
return checkAvailability(pseudo, CHECK_PSEUDO_AVAILABLE_QUERY);
}
/**
* Check if an email is available
*
* @param email The email to check
* @return True if the email is available, false if it's already taken
*/
public boolean checkEmailAvailability(String email) {
return checkAvailability(email, CHECK_EMAIL_AVAILABLE_QUERY);
}
private boolean checkAvailability(String queriedString, String correspondingQuery) {
try {
PreparedStatement statement = con.prepareStatement(correspondingQuery);
statement.setString(1, queriedString);
ResultSet result = statement.executeQuery();
return !result.next();
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* Register a new user
*
* @param pseudo The pseudo of the user
* @param email The email of the user
* @param password The password of the user
* @param firstname The firstname of the user
* @param lastname The lastname of the user
* @param description The description of the user
* @param group The group of the user
* @param avatar The avatar of the user
* @return True if the user was registered, false if an error occurred
*/
public boolean register(String pseudo, String email, String password, String firstname, String lastname, String description, String group, String avatar) {
Hash hash = Password.hash(password).withArgon2();
try {
PreparedStatement statement = con.prepareStatement(REGISTER_QUERY);
statement.setString(1, pseudo);
statement.setString(2, email);
statement.setString(3, hash.toString());
statement.setString(4, firstname);
statement.setString(5, lastname);
statement.setString(6, description);
statement.setString(7, group);
statement.setString(8, avatar);
return statement.executeUpdate() == 1;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* Login a user
*
* @param email The email of the user
* @param password The password of the user
* @return True if the user's information are correct, false otherwise (or if an error occurred)
*/
public boolean login(String email, String password) {
try {
PreparedStatement statement = con.prepareStatement(PASSWORD_FOR_EMAIL_QUERY);
statement.setString(1, email);
statement.setString(2, password);
ResultSet result = statement.executeQuery();
if (result.next()) {
String hashedPassword = result.getString("passwd");
return Password.check(password, hashedPassword).withArgon2();
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}