Bladeren bron

Error messages and propper redirect on failed login.

Iñigo Valentin 5 jaren geleden
bovenliggende
commit
ade1ed00e3

+ 13 - 5
application/Controller.php

@@ -40,9 +40,19 @@ class Controller extends Context{
      */
     private $context;
     
+    /**
+     * @var Command to execute (firs part of the url in the domain).
+     */
     private $command = "";
-    
+
+    /**
+     * @var Response status
+     */
     private $status = 201;
+
+    /**
+     * @var Response message.
+     */
     private $message = "OK";
 
     /**
@@ -163,9 +173,8 @@ class Controller extends Context{
             case "ERROR": // Error page
                 break;
             default: // Unknown page
-                
         }
-        
+
         if ($player_id != null){
             $this->context->set_player(new Player($player_id));
             if ($this->context->get_player()->get_id() == null){
@@ -174,7 +183,7 @@ class Controller extends Context{
                 return;
             }
         }
-        
+
         if ($auth_required && $this->context->get_user() == null){
             $this->status = 401;
             $this->message = "Authentication required to perform this action";
@@ -206,7 +215,6 @@ class Controller extends Context{
      * @return void|number
      */
     public function action(){
-        
         // If there was an error in preparation, it's time to throw it.
         if ($this->status >= 400 && $this->status < 500){
             require_once(PATH::PAGE . "Error_Page.php");

+ 16 - 10
application/action/Login_Action.php

@@ -32,22 +32,28 @@ class Login_Action extends Action{
         if (!isset($_POST['uname'], $_POST['password'])){
             $this->code = 401;
             $this->message = "Invalid username / password.";
-            header("Location: " . URL::BASE . "login/");
-            return;
+            http_response_code(401);
+            header("Location: " . URL::BASE . "login/?status=401");
+            die(); // So the erorr is not handled by the controller.
+            return 401;
         }
         $uname = SQLite3::escapeString($_POST['uname']);
         if (strlen($uname) == 0){
             $this->code = 401;
             $this->message = "Invalid username / password.";
-            header("Location: " . URL::BASE . "login/");
-            return;
+            http_response_code(401);
+            header("Location: " . URL::BASE . "login/?status=401");
+            die(); // So the erorr is not handled by the controller.
+            return 401;
         }
         $password = SQLite3::escapeString($_POST['password']);
         if (strlen($password) == 0){
             $this->code = 401;
             $this->message = "Invalid username / password.";
-            header("Location: " . URL::BASE . "login/");
-            return;
+            http_response_code(401);
+            header("Location: " . URL::BASE . "login/?status=401");
+            die(); // So the erorr is not handled by the controller.
+            return 401;
         }
         $password = hash('sha256', $password);
         $statement = get_context()->get_db()->prepare("
@@ -70,8 +76,9 @@ class Login_Action extends Action{
         if (!$r){
             $this->code = 401;
             $this->message = "Invalid username / password.";
-            header("Location: " . URL::BASE . "login/");
-            return;
+            header("Location: " . URL::BASE . "login/?status=401");
+            die(); // So the erorr is not handled by the controller.
+            return 401;
         }
         $user = $r["id"];
     
@@ -98,8 +105,7 @@ class Login_Action extends Action{
         $this->code = 204;
         $this->message = "No content.";
         header("Location: " . URL::BASE);
-        // TODO: Not redirecting?
         die();
-        return;
+        return 204;
     }
 }

+ 8 - 4
application/page/Login_Page.php

@@ -42,14 +42,18 @@ class Login_Page extends Page{
         $this->set_description("Login to SWDB");
         $this->set_canonical("login/");
         $this->add_css("login.css");
-        
-        if (http_response_code() == 401){
+        error_log("STATUS: " . filter_input(INPUT_GET, "status"));
+        if (http_response_code() == 401 || filter_input(INPUT_GET, "status") == "401"){
+            error_log("LOGIN ERROR");
             $this->is_error = true;
             $this->error_message = "Invalid credentials";
+            $this->set_message("Invalid credentials");
+        }
+        else{
+            $this->set_code(200);
+            $this->set_message("OK");
         }
         
-        $this->set_code(200);
-        $this->set_message("OK");
     }
     
     /**

+ 4 - 2
application/view/login.php

@@ -31,7 +31,7 @@
                     Select player
                 </h2>
                 <article>
-                    <!-- TODO: Action via JS, and fllback in controller -->
+                    <!-- TODO: Action via JS, and fallback in controller -->
                     <form method='POST' action='/action/login/'>
                         <table>
                             <tr>
@@ -64,7 +64,9 @@
 ?>
                                 <tr>
                                     <td class='error' colspan='2'>
-                                        <?=$page->error_message?>
+                                        <span>
+                                            <?=$page->get_error_message()?>
+                                        </span>
                                     </td>
                                 </tr>
 <?php

+ 11 - 0
public/css/login.css

@@ -11,5 +11,16 @@ form table td.input{
 form table td.submit{
     text-align: center;
 }
+form table td.error span{
+    display: block;
+    padding: 0.5em 1em;
+    text-align: center;
+    color: var(--error-color);
+    background-color: var(--error-color-background);
+    border: var(--error-border);
+    border-radius: var(--error-border-radius);
+    font-weight: bold;
+    margin: 2em auto 1em auto;
+}
 
 

+ 8 - 0
public/css/ui.css

@@ -82,6 +82,14 @@
     --input-text-shadow: 0 0 0.1em #000000;
     --input-box-shadow: 0 0 0.2em #000000;
     --input-button-padding: 0.3em 1em 0.3em 1em;
+    /**
+     * Error messages
+     */
+    --error-color: #ffffff;
+    --error-color-background: #9f1d1d;
+    --error-color-border: #ff0000;
+    --error-border: 0.2em solid var(--error-color-border);
+    --error-border-radius: 0.8em;
     /**
      * Other colors
      */