Works for now...
This commit is contained in:
parent
bcd610f36a
commit
ad26410ae7
@ -1,29 +1,66 @@
|
||||
<?php
|
||||
|
||||
// TODO: ...
|
||||
// TODO: this stinks...
|
||||
class User
|
||||
{
|
||||
private Session $session;
|
||||
|
||||
public ?bool $loggedIn;
|
||||
public ?string $username;
|
||||
public bool $loggedIn;
|
||||
public string $username;
|
||||
public string $password;
|
||||
|
||||
public function __construct(Session $session)
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user