Connexion fonctionnelle avec la BDD
This commit is contained in:
parent
8741707303
commit
d144ff9fe9
@ -40,7 +40,7 @@ public class MainVerticle extends AbstractVerticle {
|
|||||||
router.get("/objets").handler(this::getObjects);
|
router.get("/objets").handler(this::getObjects);
|
||||||
router.get("/objet").handler(this::getParticularObject);
|
router.get("/objet").handler(this::getParticularObject);
|
||||||
router.post("/signup").handler(this::handleSignup); // Route pour l'inscription
|
router.post("/signup").handler(this::handleSignup); // Route pour l'inscription
|
||||||
router.post("/signup").handler(this::get)
|
router.post("/login").handler(this::handleLogin); // Route pour la connexion
|
||||||
|
|
||||||
// Création du serveur HTTP
|
// Création du serveur HTTP
|
||||||
vertx.createHttpServer()
|
vertx.createHttpServer()
|
||||||
@ -171,6 +171,64 @@ public class MainVerticle extends AbstractVerticle {
|
|||||||
.setStatusCode(500)
|
.setStatusCode(500)
|
||||||
.end(new JsonObject().put("error", "Erreur d'inscription").encode());
|
.end(new JsonObject().put("error", "Erreur d'inscription").encode());
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleLogin(RoutingContext context) {
|
||||||
|
JsonObject body = context.body().asJsonObject();
|
||||||
|
|
||||||
|
if (body == null) {
|
||||||
|
context.response()
|
||||||
|
.setStatusCode(400)
|
||||||
|
.end(new JsonObject().put("error", "Requête invalide").encode());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Received login request: " + body.encodePrettily());
|
||||||
|
|
||||||
|
String email = body.getString("email");
|
||||||
|
String password = body.getString("password");
|
||||||
|
|
||||||
|
if (email == null || password == null) {
|
||||||
|
context.response()
|
||||||
|
.setStatusCode(400)
|
||||||
|
.end(new JsonObject().put("error", "Email et mot de passe requis").encode());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
databaseService.pool
|
||||||
|
.preparedQuery("SELECT password FROM users WHERE email = ?") // Requête sql qui evite les injections
|
||||||
|
.execute(Tuple.of(email)) // Remplace le ? par la valeur de l'email
|
||||||
|
.onSuccess(result -> { //Si la requête s'exécute bien , on obtient le resultat dans result
|
||||||
|
if (result.rowCount() == 0) { // Cas : Aucun utilisateur trouvé
|
||||||
|
context.response()
|
||||||
|
.setStatusCode(401)
|
||||||
|
.end(new JsonObject().put("error", "Email ou mot de passe incorrect").encode());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Row row = result.iterator().next();
|
||||||
|
String storedHashedPassword = row.getString("password");
|
||||||
|
|
||||||
|
// Vérification du mot de passe avec BCrypt
|
||||||
|
BCrypt.Result verification = BCrypt.verifyer().verify(password.toCharArray(), storedHashedPassword);
|
||||||
|
|
||||||
|
if (verification.verified) {
|
||||||
|
context.response()
|
||||||
|
.setStatusCode(200)
|
||||||
|
.end(new JsonObject().put("message", "Connexion réussie").encode());
|
||||||
|
} else {
|
||||||
|
context.response()
|
||||||
|
.setStatusCode(401)
|
||||||
|
.end(new JsonObject().put("error", "Email ou mot de passe incorrect").encode());
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.onFailure(err -> {
|
||||||
|
System.err.println("Erreur de connexion : " + err.getMessage());
|
||||||
|
context.response()
|
||||||
|
.setStatusCode(500)
|
||||||
|
.end(new JsonObject().put("error", "Erreur serveur").encode());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user