From 7b65a4bd23e67b19ae148ee839b23f02a57ac39e Mon Sep 17 00:00:00 2001 From: samuel Date: Wed, 15 Nov 2023 14:24:53 +0100 Subject: [PATCH] register finish + login to do --- .nfs0000000006fb02b80000003f | 0 src/Controller/AuthController.php | 10 +++++----- src/Gateway/AuthGateway.php | 16 +++++++++++++--- src/Model/AuthModel.php | 6 +++++- src/Views/display_register_confirm.html.twig | 8 ++++---- 5 files changed, 27 insertions(+), 13 deletions(-) delete mode 100644 .nfs0000000006fb02b80000003f diff --git a/.nfs0000000006fb02b80000003f b/.nfs0000000006fb02b80000003f deleted file mode 100644 index e69de29..0000000 diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index 864190e..48217eb 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -39,8 +39,8 @@ class AuthController { if (!empty($fails)) { $bad_fields = []; - foreach ($fails as $err){ - if ($err instanceof FieldValidationFail){ + foreach ($fails as $err) { + if ($err instanceof FieldValidationFail) { $bad_fields[] = $err->getFieldName(); } } @@ -50,12 +50,12 @@ class AuthController { $fails = $this->model->validationRegister($request['username'], $request["password"], $request['confirmpassword'], $request['email']); if (empty($fails)) { $results = $this->model->getUserFields($request['email']); - return ViewHttpResponse::twig("display_register_confirm.html.twig", ['results' => $results]); + return ViewHttpResponse::twig("display_register_confirm.html.twig", ['username' => $results['username'], 'email' => $results['email']]); } $bad_fields = []; - foreach ($fails as $err){ - if ($err instanceof FieldValidationFail){ + foreach ($fails as $err) { + if ($err instanceof FieldValidationFail) { $bad_fields[] = $err->getFieldName(); } } diff --git a/src/Gateway/AuthGateway.php b/src/Gateway/AuthGateway.php index c0cafe7..f128a20 100644 --- a/src/Gateway/AuthGateway.php +++ b/src/Gateway/AuthGateway.php @@ -15,14 +15,24 @@ class AuthGateway { $this->con = $con; } - public function insertAccount(string $username, string $hash, string $email) { + public function mailExist(string $email):bool{ + return $this->getUserFields($email) != null; + } + + + public function insertAccount(string $username, string $hash, string $email) { $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 getUserFields (string $email):array{ - return $this->con->fetch ("SELECT username,email FROM AccountUser WHERE email = :email",[':email'=>[$email, PDO::PARAM_STR]]); + + public function getUserFields (string $email): ?array { + $results = $this->con->fetch ("SELECT username,email FROM AccountUser WHERE email = :email",[':email'=>[$email, PDO::PARAM_STR]]); + $firstRow = $results[0] ?? null; + return $firstRow; } + + } \ No newline at end of file diff --git a/src/Model/AuthModel.php b/src/Model/AuthModel.php index 430fadc..8853962 100644 --- a/src/Model/AuthModel.php +++ b/src/Model/AuthModel.php @@ -24,7 +24,11 @@ class AuthModel { $errors[] = new FieldValidationFail("confirmpassword","passwords not equals"); } - else{ + if ($this->gateway->mailExist($email)){ + $errors[] = new FieldValidationFail("email","email already exist"); + } + + if(empty($errors)){ $hash = password_hash($password,PASSWORD_DEFAULT); $this->gateway->insertAccount($username,$hash,$email); } diff --git a/src/Views/display_register_confirm.html.twig b/src/Views/display_register_confirm.html.twig index 1b73b31..115676b 100644 --- a/src/Views/display_register_confirm.html.twig +++ b/src/Views/display_register_confirm.html.twig @@ -8,10 +8,10 @@

Nouveau Compte

-{% for row in results %} -

Votre pseudo : {{ row['username']}}

-

Votre Email : {{ row['email']}}

-{% endfor %} + +

Votre pseudo : {{ username }}

+

Votre Email : {{ email }}

+ \ No newline at end of file