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/UserAccount.php | 52 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Digitigrade/Model/UserAccount.php (limited to 'Digitigrade/Model/UserAccount.php') diff --git a/Digitigrade/Model/UserAccount.php b/Digitigrade/Model/UserAccount.php new file mode 100644 index 0000000..3d0e2a4 --- /dev/null +++ b/Digitigrade/Model/UserAccount.php @@ -0,0 +1,52 @@ +email = $email; + $user->passwordHash = defaults_password_hash($password); + $user->actor = null; + $user->save(); + return $user; + } + + public function createLinkedActor(string $handle) { + $this->actor = Actor::create($handle); + $this->save(); + } + + protected function getUpdateWhereClause(\PDO $db): ?string { + if (isset($this->id) && self::find($this->{'id'}) != null) { + return 'id = ' . $this->{'id'}; + } + if (self::findByEmail($this->email) != null) { + return 'email = ' . $db->quote($this->email); + } + return null; + } + + public static function findByEmail(string $email): ?self { + return self::findWhere('email = ?', [$email]); + } + + public static function findByCurrentSession(): ?self { + if (!isset($_COOKIE['digitigrade-session'])) { + return null; + } + $token = $_COOKIE['digitigrade-session']; + return Session::findByToken($token)?->userAccount; + } + + public function verifyPassword(#[\SensitiveParameter] $attemptedPassword): bool { + return password_verify($attemptedPassword, $this->passwordHash); + } +} \ No newline at end of file -- cgit v1.3