This commit is contained in:
William 2022-01-26 20:28:00 +01:00
parent 34a963bb8d
commit f4ef0b648e
3 changed files with 25 additions and 21 deletions

View File

@ -48,6 +48,15 @@ class Session
// TODO: throwaway code; rewrite for readability and also implement proper flashing by removing messages after one request // TODO: throwaway code; rewrite for readability and also implement proper flashing by removing messages after one request
public function flash(string $msg, string $type = 'info'): void public function flash(string $msg, string $type = 'info'): void
{ {
$types = [
"info",
"success",
"danger",
"warning"
];
if (!in_array($type, $types)) {
throw new Exception("Flash type: \"$type\" does not exist");
}
$key = 'flashed_messages'; $key = 'flashed_messages';
if (!$this->has($key)) if (!$this->has($key))
{ {

View File

@ -5,31 +5,26 @@ class User
{ {
private Session $session; private Session $session;
public bool $loggedIn; public ?bool $loggedIn;
public string $username; public ?string $username;
public string $password; public ?string $password;
public function __construct(Session $session) public function __construct(Session $session)
{ {
$this->session = $session; $this->session = $session;
$this->setProperties(); $this->setProps();
} }
private function setProperties(): void private function setProps(): void
{ {
if ($this->session->get('loggedIn')) $this->loggedIn = $this->session->get('loggedIn');
{ $this->username = $this->session->get('username');
$this->loggedIn = $this->session->get('loggedIn'); $this->password = $this->session->get('password');
$this->username = $this->session->get('username');
$this->password = $this->session->get('password');
if (!$this->authenticate($this->username, $this->password)) if ($this->loggedIn && !$this->authenticate($this->username, $this->password))
{ {
$this->logout(); $this->logout();
$this->session->flash('Kontodetaljer endret, vennligst logg inn igjen', 'warning'); $this->session->flash('Kontodetaljer endret, vennligst logg inn igjen', 'warning');
}
} else {
$this->loggedIn = FALSE;
} }
} }
@ -41,7 +36,7 @@ class User
$this->session->set('loggedIn', TRUE); $this->session->set('loggedIn', TRUE);
$this->session->set('username', $username); $this->session->set('username', $username);
$this->session->set('password', $password); $this->session->set('password', $password);
$this->setProperties(); $this->setProps();
return TRUE; return TRUE;
} }
return FALSE; return FALSE;
@ -59,9 +54,9 @@ class User
public function logout(): void public function logout(): void
{ {
$this->session->set('loggedIn', FALSE); $this->session->remove('loggedIn');
$this->session->remove('username'); $this->session->remove('username');
$this->session->remove('password'); $this->session->remove('password');
$this->setProperties(); $this->setProps();
} }
} }

View File

@ -6,6 +6,6 @@ if (!$app->user->loggedIn)
$app->redirect('login.php'); $app->redirect('login.php');
} }
$app->session->clear(); $app->user->logout();
$app->session->flash('Du har blitt logget av'); $app->session->flash('Du har blitt logget av');
$app->redirect("login.php"); $app->redirect("login.php");