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 --- Digitigrade/Model/Session.php | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Digitigrade/Model/Session.php (limited to 'Digitigrade/Model/Session.php') diff --git a/Digitigrade/Model/Session.php b/Digitigrade/Model/Session.php new file mode 100644 index 0000000..31fb8bb --- /dev/null +++ b/Digitigrade/Model/Session.php @@ -0,0 +1,43 @@ +userAccount = $user; + $session->token = random_printable_bytes(); + $session->save(); + return $session; + } + + protected function getUpdateWhereClause(\PDO $db): ?string { + if (self::findByToken($this->token) != null) { + return 'token = ' . $db->quote($this->token); + } + return null; + } + + public static function findByToken(#[\SensitiveParameter] string $token): ?self { + return self::findWhere('token = ?', [$token]); + } + + public static function findAllByAccount(UserAccount $userAccount): array { + return self::findAllWhere('user_account = ?', [$userAccount->id]); + } + + public function setCookie(bool $rememberSession) { + setcookie('digitigrade-session', $this->token, [ + 'expires' => $rememberSession ? (new \DateTimeImmutable('1 year'))->getTimestamp() : null, + 'httponly' => true, + 'samesite' => 'Lax', + 'path' => '/', + 'secure' => GlobalConfig::getInstance()->isHttps() + ]); + } +} \ No newline at end of file -- cgit v1.3