commit 238ed149ec84022fd254b7913d3489ef2114968d Author: Jordy van Zeeland Date: Mon Aug 12 17:08:06 2024 +0200 Router and Auth classes diff --git a/auth.php b/auth.php new file mode 100644 index 0000000..9d2ab00 --- /dev/null +++ b/auth.php @@ -0,0 +1,69 @@ +pdo = new PDO($dsn, $username. $password, [ + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + ]); + + session_start(); + } + + /** + * Method to autenticate the user based on the provided routes. + */ + + public function login($username, $password){ + $sql = "SELECT * FROM users WHERE username = :username or email = :email LIMIT 1"; + + /** + * Prepare the SQL query for execution. + * Then txecute the query with the provided username and password parameters and + * fetch the user record from the database. + */ + + $query = $this->pdo->prepare($query); + $query->execute([ + ":username" => $username, + ":password" => $password + ]); + + $user = $query->fetch(); + + /** + * Check if the provided password is verified against the hashed password in the database. + * If correct, store user information in a session. + * + * Return true indicating a successful login + */ + + if($user && password_verify($password, $user['password'])){ + $_SESSION['user_id'] = $user['id']; + $_SESSION['username'] = $user['username']; + + return true; + } + + return false; + } + +} + +?> \ No newline at end of file diff --git a/router.php b/router.php new file mode 100644 index 0000000..fe14a7e --- /dev/null +++ b/router.php @@ -0,0 +1,54 @@ +uri, $routes)){ + // If it exists, require (include and execute) the file corresponding to the route + require $routes[$uri]; + }else{ + // If the route doesn't exist, call the abort method to handle the error + $this->abort(); + } + } + + /** + * Method to handle HTTP errors + * Default is 404 - Not Found + */ + + public function abort($code = 404){ + // Set the HTTP response code to the specified value + http_response_code($code); + // Include and execute the corresponding error view (e.g., "views/404.php") + require "views/{$code}.php"; + // Terminate the script execution + die(); + } +} + +// $routes = [ +// '/' => 'controllers/index.php', +// '/about' => 'controllers/about.php', +// '/contact' => 'controllers/contact.php', +// ]; + +// $router = (new Router)->routeToController($routes); + +?> \ No newline at end of file