aboutsummaryrefslogtreecommitdiffhomepage
path: root/routes/user_auth.php
diff options
context:
space:
mode:
authorwinter2024-12-31 22:30:16 +0000
committerwinter2024-12-31 22:31:26 +0000
commit4cd7017da9a37e5b9f38c3f50dd1c904ea41cbcb (patch)
treed77f1fd223dae44f40b22c9810aeba238733310d /routes/user_auth.php
parent982e6213622bcb78a40d17f54cda27225a1d84b3 (diff)
implement user accounts and login
Diffstat (limited to 'routes/user_auth.php')
-rw-r--r--routes/user_auth.php32
1 files changed, 32 insertions, 0 deletions
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 @@
+<?php
+
+use Digitigrade\HttpResponseStatus\TemporaryRedirect;
+use Digitigrade\Model\Session;
+use Digitigrade\Model\UserAccount;
+use Digitigrade\Router;
+
+Router::getInstance()->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