Added the /login and /register routes
This commit is contained in:
parent
65177b21fc
commit
bbc752500a
4 changed files with 106 additions and 1 deletions
BIN
password4j-1.6.3.jar
Normal file
BIN
password4j-1.6.3.jar
Normal file
Binary file not shown.
|
@ -2,6 +2,8 @@ package be.jeffcheasey88.peeratcode.repository;
|
|||
|
||||
import be.jeffcheasey88.peeratcode.model.Chapter;
|
||||
import be.jeffcheasey88.peeratcode.model.Puzzle;
|
||||
import com.password4j.Hash;
|
||||
import com.password4j.Password;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
|
@ -14,7 +16,11 @@ public class DatabaseRepo {
|
|||
private static final String SPECIFIC_PUZZLE_QUERY = "SELECT * FROM puzzle WHERE id_puzzle = ?";
|
||||
private static final String SPECIFIC_CHAPTER_QUERY = "SELECT * FROM chapter WHERE id_chapter = ?";
|
||||
private static final String PUZZLES_IN_CHAPTER_QUERY = "SELECT * FROM puzzle WHERE fk_chapter = ?";
|
||||
public static final String ALL_CHAPTERS_QUERY = "SELECT * FROM chapter";
|
||||
private static final String ALL_CHAPTERS_QUERY = "SELECT * FROM chapter";
|
||||
private static final String CHECK_PSEUDO_AVAILABLE_QUERY = "SELECT * FROM user WHERE pseudo = ?";
|
||||
private static final String CHECK_EMAIL_AVAILABLE_QUERY = "SELECT * FROM user WHERE email = ?";
|
||||
private static final String REGISTER_QUERY = "INSERT INTO user (pseudo, email, passwd, firstname, lastname, description, `group`, avatar) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
private static final String PASSWORD_FOR_EMAIL_QUERY = "SELECT passwd FROM user WHERE pseudo = ?";
|
||||
private final Connection con;
|
||||
|
||||
public DatabaseRepo(Connection con) {
|
||||
|
|
39
src/be/jeffcheasey88/peeratcode/routes/Login.java
Normal file
39
src/be/jeffcheasey88/peeratcode/routes/Login.java
Normal file
|
@ -0,0 +1,39 @@
|
|||
package be.jeffcheasey88.peeratcode.routes;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepo;
|
||||
import be.jeffcheasey88.peeratcode.webserver.HttpReader;
|
||||
import be.jeffcheasey88.peeratcode.webserver.HttpUtil;
|
||||
import be.jeffcheasey88.peeratcode.webserver.HttpWriter;
|
||||
import be.jeffcheasey88.peeratcode.webserver.Response;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class Login implements Response {
|
||||
private final DatabaseRepo databaseRepo;
|
||||
|
||||
public Login(DatabaseRepo databaseRepo) {
|
||||
this.databaseRepo = databaseRepo;
|
||||
}
|
||||
@Override
|
||||
public void exec(Matcher matcher, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
HttpUtil.skipHeaders(reader);
|
||||
JSONObject informations = (JSONObject) HttpUtil.readJson(reader);
|
||||
if (informations != null) {
|
||||
String pseudo = (String) informations.get("pseudo");
|
||||
String password = (String) informations.get("passwd");
|
||||
boolean wellLogged = databaseRepo.login(pseudo, password);
|
||||
if (!wellLogged) {
|
||||
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||
} else {
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pattern getPattern() {
|
||||
return Pattern.compile("^\\/login$");
|
||||
}
|
||||
}
|
60
src/be/jeffcheasey88/peeratcode/routes/Register.java
Normal file
60
src/be/jeffcheasey88/peeratcode/routes/Register.java
Normal file
|
@ -0,0 +1,60 @@
|
|||
package be.jeffcheasey88.peeratcode.routes;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepo;
|
||||
import be.jeffcheasey88.peeratcode.webserver.HttpReader;
|
||||
import be.jeffcheasey88.peeratcode.webserver.HttpUtil;
|
||||
import be.jeffcheasey88.peeratcode.webserver.HttpWriter;
|
||||
import be.jeffcheasey88.peeratcode.webserver.Response;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class Register implements Response {
|
||||
|
||||
private final DatabaseRepo databaseRepo;
|
||||
|
||||
public Register(DatabaseRepo databaseRepo) {
|
||||
this.databaseRepo = databaseRepo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exec(Matcher matcher, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
HttpUtil.skipHeaders(reader);
|
||||
JSONObject informations = (JSONObject) HttpUtil.readJson(reader);
|
||||
if (informations != null) {
|
||||
String pseudo = (String) informations.get("pseudo");
|
||||
String email = (String) informations.get("email");
|
||||
String password = (String) informations.get("passwd");
|
||||
String firstname = (String) informations.get("firstname");
|
||||
String lastname = (String) informations.get("lastname");
|
||||
String description = (String) informations.get("description");
|
||||
String group = (String) informations.get("group");
|
||||
String avatar = (String) informations.get("avatar");
|
||||
|
||||
boolean pseudoAvailable = databaseRepo.checkPseudoAvailability(pseudo);
|
||||
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
|
||||
if (pseudoAvailable && emailAvailable) {
|
||||
boolean wellRegistered = databaseRepo.register(pseudo, email, password, firstname, lastname, description, group, avatar);
|
||||
if (!wellRegistered) {
|
||||
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
||||
writer.write("Error while registering");
|
||||
} else {
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||
writer.write("OK");
|
||||
}
|
||||
} else {
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||
JSONObject error = new JSONObject();
|
||||
error.put("username_valid", pseudoAvailable);
|
||||
error.put("email_valid", emailAvailable);
|
||||
writer.write(error.toJSONString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pattern getPattern() {
|
||||
return Pattern.compile("^\\/register$");
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue