mirror of
https://github.com/standardebooks/web.git
synced 2025-07-10 00:30:28 -04:00
Create cookie-based login and authentication system
This commit is contained in:
parent
45221365b5
commit
0bc3dc3830
46 changed files with 528 additions and 195 deletions
82
lib/Session.php
Normal file
82
lib/Session.php
Normal file
|
@ -0,0 +1,82 @@
|
|||
<?
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Safe\DateTime;
|
||||
|
||||
/**
|
||||
* @property User $User
|
||||
* @property PollItem $PollItem
|
||||
* @property string $Url
|
||||
*/
|
||||
class Session extends PropertiesBase{
|
||||
public $UserId;
|
||||
protected $_User = null;
|
||||
public $Created;
|
||||
public $SessionId;
|
||||
public $_Url;
|
||||
|
||||
|
||||
// *******
|
||||
// GETTERS
|
||||
// *******
|
||||
|
||||
protected function GetUrl(): string{
|
||||
if($this->_Url === null){
|
||||
$this->_Url = '/sessions/' . $this->SessionId;
|
||||
}
|
||||
|
||||
return $this->_Url;
|
||||
}
|
||||
|
||||
|
||||
// *******
|
||||
// METHODS
|
||||
// *******
|
||||
|
||||
public function Create(?string $email = null): void{
|
||||
$this->User = User::GetIfRegistered($email);
|
||||
$this->UserId = $this->User->UserId;
|
||||
|
||||
$existingSessions = Db::Query('SELECT SessionId, Created from Sessions where UserId = ?', [$this->UserId]);
|
||||
|
||||
if(sizeof($existingSessions) > 0){
|
||||
$this->SessionId = $existingSessions[0]->SessionId;
|
||||
$this->Created = $existingSessions[0]->Created;
|
||||
}
|
||||
else{
|
||||
$uuid = Uuid::uuid4();
|
||||
$this->SessionId = $uuid->toString();
|
||||
$this->Created = new DateTime();
|
||||
Db::Query('INSERT into Sessions (UserId, SessionId, Created) values (?, ?, ?)', [$this->UserId, $this->SessionId, $this->Created]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function GetLoggedInUser(): ?User{
|
||||
$sessionId = HttpInput::Str(COOKIE, 'sessionid');
|
||||
|
||||
if($sessionId !== null){
|
||||
$result = Db::Query('select u.* from Users u inner join Sessions s using (UserId) where s.SessionId = ?', [$sessionId], 'User');
|
||||
|
||||
if(sizeof($result) > 0){
|
||||
// Refresh the login cookie for another 2 weeks
|
||||
setcookie('sessionid', $sessionId, time() + 60 * 60 * 24 * 14 * 1, '/', SITE_DOMAIN, true, false); // Expires in two weeks
|
||||
return $result[0];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function Get(?string $sessionId): Session{
|
||||
if($sessionId === null){
|
||||
throw new Exceptions\InvalidSessionException();
|
||||
}
|
||||
|
||||
$result = Db::Query('SELECT * from Sessions where SessionId = ?', [$sessionId], 'Session');
|
||||
|
||||
if(sizeof($result) == 0){
|
||||
throw new Exceptions\InvalidSessionException();
|
||||
}
|
||||
|
||||
return $result[0];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue