userTable = $userTable; $this->session = $session; } public function login(string $username, string $password): ?User { if (empty($username) || empty($password)) { return null; } /** @var \App\Auth\User $user */ $user = $this->userTable->findBy('username', $username); if ($user && password_verify($password, $user->password)) { $this->setUser($user); return $user; } return null; } public function logout(): void { $this->session->delete('auth.user'); } /** * @return User|null */ public function getUser(): ?User { if ($this->user) { return $this->user; } $userId = $this->session->get('auth.user'); if ($userId) { try { $this->user = $this->userTable->find($userId); return $this->user; } catch (NoRecordException $exception) { $this->session->delete('auth.user'); return null; } } return null; } public function setUser(\App\Auth\User $user): void { $this->session->set('auth.user', $user->id); $this->user = $user; } }