authentication completed, view more pretty in progress
continuous-integration/drone/push Build is passing Details

pull/12/head
samuel 2 years ago
parent c9a86f02f2
commit a4a6b5d6f2

@ -39,6 +39,7 @@ $router->map("POST", "/submit-twig", fn() => $sampleFormController->submitFormTw
$router->map("GET", "/register", fn() => $authController->displayRegister()); $router->map("GET", "/register", fn() => $authController->displayRegister());
$router->map("POST", "/register", fn() => $authController->confirmRegister($_POST)); $router->map("POST", "/register", fn() => $authController->confirmRegister($_POST));
$router->map("GET", "/login", fn() => $authController->displayLogin()); $router->map("GET", "/login", fn() => $authController->displayLogin());
$router->map("POST", "/login", fn() => $authController->displayLoginPassed($_POST));
$router->map("GET", "/tactic/new", fn() => $editorController->makeNew()); $router->map("GET", "/tactic/new", fn() => $editorController->makeNew());
$router->map("GET", "/tactic/[i:id]/edit", fn(int $id) => $editorController->openEditorFor($id)); $router->map("GET", "/tactic/[i:id]/edit", fn(int $id) => $editorController->openEditorFor($id));

@ -50,7 +50,7 @@ class AuthController {
$fails = $this->model->validationRegister($request['username'], $request["password"], $request['confirmpassword'], $request['email']); $fails = $this->model->validationRegister($request['username'], $request["password"], $request['confirmpassword'], $request['email']);
if (empty($fails)) { if (empty($fails)) {
$results = $this->model->getUserFields($request['email']); $results = $this->model->getUserFields($request['email']);
return ViewHttpResponse::twig("display_register_confirm.html.twig", ['username' => $results['username'], 'email' => $results['email']]); return ViewHttpResponse::twig("display_auth_confirm.html.twig", ['username' => $results['username'], 'email' => $results['email']]);
} }
$bad_fields = []; $bad_fields = [];
@ -73,9 +73,19 @@ class AuthController {
"email" => [Validators::regex("/@/")] "email" => [Validators::regex("/@/")]
]); ]);
$results = $this->model->getUserFields($request['email']);
return ViewHttpResponse::twig("display_login_passed.html.twig",[['username' => $results['username'], 'email' => $results['email']]]); $fails = $this->model->validationLogin($request['email'],$request['password']);
$bad_fields = [];
foreach ($fails as $err){
if ($err instanceof FieldValidationFail){
$bad_fields [] = $err->getFieldName();
}
}
if (empty($fails)){
$results = $this->model->getUserFields($request['email']);
return ViewHttpResponse::twig("display_auth_confirm.html.twig",['username' => $results['username'], 'email' => $results['email']]);
}
return ViewHttpResponse::twig("display_login.html.twig",['bad_fields' => $bad_fields]);
} }
} }

@ -25,6 +25,11 @@ class AuthGateway {
$this->con->exec("INSERT INTO AccountUser VALUES (:username,:hash,:email)", [':username' => [$username, PDO::PARAM_STR],':hash'=> [$hash, PDO::PARAM_STR],':email'=>[$email, PDO::PARAM_STR]]); $this->con->exec("INSERT INTO AccountUser VALUES (:username,:hash,:email)", [':username' => [$username, PDO::PARAM_STR],':hash'=> [$hash, PDO::PARAM_STR],':email'=>[$email, PDO::PARAM_STR]]);
} }
public function getUserHash(string $email):string{
$results = $this->con->fetch ("SELECT hash FROM AccountUser WHERE email = :email",[':email'=>[$email, PDO::PARAM_STR]]);
return $results[0]['hash'];
}
public function getUserFields (string $email): ?array { public function getUserFields (string $email): ?array {
$results = $this->con->fetch ("SELECT username,email FROM AccountUser WHERE email = :email",[':email'=>[$email, PDO::PARAM_STR]]); $results = $this->con->fetch ("SELECT username,email FROM AccountUser WHERE email = :email",[':email'=>[$email, PDO::PARAM_STR]]);

@ -20,6 +20,7 @@ class AuthModel {
public function validationRegister(string $username, string $password, string $confirmPassword,string $email): array { public function validationRegister(string $username, string $password, string $confirmPassword,string $email): array {
$errors = []; $errors = [];
if ($password != $confirmPassword) { if ($password != $confirmPassword) {
$errors[] = new FieldValidationFail("confirmpassword","passwords not equals"); $errors[] = new FieldValidationFail("confirmpassword","passwords not equals");
} }
@ -41,5 +42,24 @@ class AuthModel {
} }
public function validationLogin(string $email,string $password): array{
$errors = [];
if (!$this->gateway->mailExist($email)) {
$errors[] = new FieldValidationFail("email", "email doesnt exists");
return $errors;
}
$hash = $this->gateway->getUserHash($email);
if (!password_verify($password, $hash)) {
$errors[] = new FieldValidationFail("password", "invalid password");
}
return $errors;
}
} }

@ -1,19 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Compte</title>
</head>
<body>
<center><h2>Votre Compte</h2></center>
{% for v in results %}
<p>username: {{ v.name }}</p>
<p>description: {{ v.description }}</p>
{% endfor %}
</body>
</html>
Loading…
Cancel
Save