diff --git a/app/core/User.php b/app/core/User.php index a38e323..36cc687 100644 --- a/app/core/User.php +++ b/app/core/User.php @@ -1,29 +1,66 @@ session = $session; - - $this->loggedIn = $this->session->get('loggedIn'); - $this->username = $this->session->get('username'); + $this->setProperties(); } + private function setProperties(): void + { + if ($this->session->get('loggedIn')) + { + $this->loggedIn = $this->session->get('loggedIn'); + $this->username = $this->session->get('username'); + $this->password = $this->session->get('password'); + + if (!$this->authenticate($this->username, $this->password)) { + $this->logout(); + $this->session->flash('Kontodetaljer endret, vennligst logg inn igjen', 'warning'); + } + } else { + $this->loggedIn = FALSE; + } + } + + // Set session if user and password match public function login(string $username, string $password): bool { - if ($username === 'William' && $password === 'William') + if ($this->authenticate($username, $password)) { $this->session->set('loggedIn', TRUE); - $this->session->set('username', 'William'); + $this->session->set('username', $username); + $this->session->set('password', $password); + $this->setProperties(); return TRUE; } return FALSE; } + + // Check if user and pass match + private function authenticate(string $username, string $password): bool + { + if ($username === 'Willaiam' && $password === 'William') + { + return TRUE; + } + return FALSE; + } + + public function logout(): void + { + $this->session->set('loggedIn', FALSE); + $this->session->remove('username', FALSE); + $this->session->remove('password', FALSE); + $this->setProperties(); + } } \ No newline at end of file