diff options
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/homepage.php | 3 | ||||
| -rw-r--r-- | routes/user_auth.php | 32 |
2 files changed, 34 insertions, 1 deletions
diff --git a/routes/homepage.php b/routes/homepage.php index c147d67..db8ea72 100644 --- a/routes/homepage.php +++ b/routes/homepage.php @@ -5,7 +5,8 @@ use Digitigrade\HttpResponseStatus\TemporaryRedirect; use Digitigrade\Router; Router::getInstance()->mount('/', function (array $args) { - if (str_contains($_SERVER['HTTP_ACCEPT'] ?? '', 'text/html')) { + $accept = $_SERVER['HTTP_ACCEPT'] ?? ''; + if (str_contains($accept, 'text/html') || str_contains($accept, '*/*')) { throw new TemporaryRedirect('/feed/global'); } throw new NotFound(); 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 |
