From ad26410ae7e64336dfb7e5e3f81f5bfb43156d7c Mon Sep 17 00:00:00 2001 From: William Date: Tue, 25 Jan 2022 17:50:32 +0100 Subject: [PATCH] Works for now... --- app/core/User.php | 55 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 9 deletions(-) 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