From 4cd7017da9a37e5b9f38c3f50dd1c904ea41cbcb Mon Sep 17 00:00:00 2001 From: winter Date: Tue, 31 Dec 2024 22:30:16 +0000 Subject: implement user accounts and login --- routes/user_auth.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 routes/user_auth.php (limited to 'routes/user_auth.php') diff --git a/routes/user_auth.php b/routes/user_auth.php new file mode 100644 index 0000000..afdf8d8 --- /dev/null +++ b/routes/user_auth.php @@ -0,0 +1,32 @@ +mount('/login', function (array $args) { + if (UserAccount::findByCurrentSession() != null) { + // already logged in + throw new TemporaryRedirect('/'); + } + $failedVerification = false; + if (isset($_POST['email'], $_POST['password'])) { + $failedVerification = true; + // try to log in? + $user = UserAccount::findByEmail($_POST['email']); + if ($user?->verifyPassword($_POST['password'])) { + // success + $session = Session::create($user); + $session->setCookie(isset($_POST['rememberSession'])); + throw new TemporaryRedirect('/'); + } + } + + if (isset($_SERVER['HTTP_HX_CURRENT_URL']) && str_ends_with($_SERVER['HTTP_HX_CURRENT_URL'], '/login')) { + // htmx form submission, therefore just return the form itself + render_template('login_form', ['email' => $_POST['email'] ?? '', 'failed' => $failedVerification]); + } else { + render_template('login_page', ['email' => $_POST['email'] ?? '', 'failed' => $failedVerification]); + } +}); \ No newline at end of file -- cgit v1.3