From 4146286432e79602e9c9dc2b87c2713f8b4fa82f Mon Sep 17 00:00:00 2001
From: Kenneth Ezekiel <88850771+KenEzekiel@users.noreply.github.com>
Date: Tue, 3 Oct 2023 14:53:05 +0700
Subject: [PATCH] feat: client error message

---
 public/css/styles.css                  | 5 +++++
 src/controllers/LoginController.php    | 7 ++++++-
 src/controllers/RegisterController.php | 7 ++++++-
 views/login.php                        | 3 +++
 views/register.php                     | 3 +++
 5 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/public/css/styles.css b/public/css/styles.css
index b8ff936..37e18c9 100644
--- a/public/css/styles.css
+++ b/public/css/styles.css
@@ -124,3 +124,8 @@ table {
   background-color: #2b5c02;
   transform: translateY(4px);
 }
+
+.error-msg {
+  color: red;
+  font-size: small;
+}
\ No newline at end of file
diff --git a/src/controllers/LoginController.php b/src/controllers/LoginController.php
index 7ca40f4..0f955b8 100644
--- a/src/controllers/LoginController.php
+++ b/src/controllers/LoginController.php
@@ -34,7 +34,12 @@ class LoginController extends BaseController
   {
     $username_email = $_POST['username-email'];
     $password = $_POST['password'];
-    $this->service->login($username_email, $password);
+    try {
+      $this->service->login($username_email, $password);
+    } catch (Exception $e) {
+      $msg = $e->getMessage();
+      parent::render(["errorMsg" => $msg], "login", "layouts/base");
+    }
     if (isset($_SESSION['user_id'])) {
       // redirect
       parent::redirect("/");
diff --git a/src/controllers/RegisterController.php b/src/controllers/RegisterController.php
index 57ee1f8..47e6b5a 100644
--- a/src/controllers/RegisterController.php
+++ b/src/controllers/RegisterController.php
@@ -28,7 +28,12 @@ class RegisterController extends BaseController
     $email = $_POST['email'];
     $password = $_POST['password'];
     $confirm_password = $_POST['confirm-password'];
-    $response = $this->service->register($username, $email, $password, $confirm_password);
+    try {
+      $response = $this->service->register($username, $email, $password, $confirm_password);
+    } catch (Exception $e) {
+      $msg = $e->getMessage();
+      parent::render(["errorMsg" => $msg], "register", "layouts/base");
+    }
     parent::redirect("/login");
   }
 }
diff --git a/views/login.php b/views/login.php
index 04bf1f4..f8d2870 100644
--- a/views/login.php
+++ b/views/login.php
@@ -1,5 +1,8 @@
 <div class="form-container">
   <h2 class="header-title">Login</h2>
+  <p class="error-msg"><?php if (isset($errorMsg)) {
+                          echo "$errorMsg";
+                        } ?></p>
   <form class="form" method="post">
     <div class="form-group">
       <label for="username-email">Username or Email</label>
diff --git a/views/register.php b/views/register.php
index 0832902..8adc093 100644
--- a/views/register.php
+++ b/views/register.php
@@ -1,5 +1,8 @@
 <div class="form-container">
   <h2 class="header-title">Register</h2>
+  <p class="error-msg"><?php if (isset($errorMsg)) {
+                          echo "$errorMsg";
+                        } ?></p>
   <form class="form" method="post">
     <div class="form-group">
       <label for="username">Username</label>
-- 
GitLab