From 38cdd1bfb2716cb3244b74aaaba230d7ce0bb624 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Thu, 21 Nov 2024 21:44:18 -0500 Subject: [PATCH] Add timestamps to teh user since its also an entity, and test the login. Add a redirect helper for the responses. --- .../20241122022752_add_time_stamps_to_users.php | 3 ++- app/src/Controller/Controller.php | 13 +++++++++++++ app/src/Controller/User/LoginUserAction.php | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/db/migrations/20241122022752_add_time_stamps_to_users.php b/app/db/migrations/20241122022752_add_time_stamps_to_users.php index 9bf1904..a71793f 100644 --- a/app/db/migrations/20241122022752_add_time_stamps_to_users.php +++ b/app/db/migrations/20241122022752_add_time_stamps_to_users.php @@ -19,6 +19,7 @@ final class AddTimeStampsToUsers extends AbstractMigration */ public function change(): void { - + $table = $this->table("users")->addTimestamps(); + $table->update(); } } diff --git a/app/src/Controller/Controller.php b/app/src/Controller/Controller.php index 8b2ac0a..7b61c2e 100644 --- a/app/src/Controller/Controller.php +++ b/app/src/Controller/Controller.php @@ -58,6 +58,19 @@ abstract class Controller ->withHeader('Content-Type', 'text/html'); } + /** + * Redirect helper for sending the client elsewhere. + * + * @param string $route The route to redirect doe + * @param int $code Default `301`, the code to respond with + */ + public function redirect(string $route, int $code = 301): Response + { + return $this->response + ->withHeader('Location', $route) + ->withStatus($code); + } + /** * @param string $html The raw Twig HTML to render * @param array $data The data to injext into the HTML diff --git a/app/src/Controller/User/LoginUserAction.php b/app/src/Controller/User/LoginUserAction.php index 4e9a159..7735e83 100644 --- a/app/src/Controller/User/LoginUserAction.php +++ b/app/src/Controller/User/LoginUserAction.php @@ -35,6 +35,6 @@ class LoginUserAction extends Controller // start the session $this->session->set('user', [ 'id' => $user->getId(), 'authenticated' => true ]); - return $this->render('dashboard.twig', [ 'logged_in' => true ]); + return $this->redirect('/dashboard', 302); } }