-WIP on Bearer token

This commit is contained in:
Dave Mc Nicoll committed 2024-05-31 12:27:07 +00:00
1 parent 0657622eab
commit c4e4db7a45
13 files changed
+377 -35

No files matched your search

+41 -32
View File
@@ -26,29 +26,12 @@ class Authenticate {
public function rememberMe() : ? UserInterface
{
$logUser = function(? int $id) {
try {
if ($id === null || null === ($user = $this->user::repository()->loadFromPk($id))) {
throw new \InvalidArgumentException(sprintf("User having id '%s' was not found.", $id));
}
}
catch(\Exception $ex) {
return null;
}
$this->user->fromArray($user);
$this->user->logged = true;
return $this->user;
};
if ( $this->session && $this->session->has("user.id") ) {
return $logUser($this->session->get("user.id"));
return $this->logUser($this->session->get("user.id"));
}
if ( $this->cookie && $this->cookie->has("user.id") ) {
return $logUser($this->cookie->get("user.id"));
return $this->logUser($this->cookie->get("user.id"));
}
return null;
@@ -86,15 +69,7 @@ class Authenticate {
$response = call_user_func_array($this->authenticationEvent, [ false, 'verifyPassword', $this->user, [ 'password' => $password ] ]);
if ( $response !== null ? $response : $this->user->verifyPassword($password) ) {
$this->user->logged = true;
if ( $this->session ) {
$this->session->set("user.id", $this->user->id);
}
if ( $this->cookie ) {
$this->cookie->set("user.id", $this->user->id);
}
$this->login();
call_user_func_array($this->authenticationEvent, [ true, 'success', $this->user ]);
}
@@ -112,8 +87,28 @@ class Authenticate {
return $this->user->logged;
}
public function login() : void
{
if ( !$this->user->isLoaded() ) {
return;
}
$this->user->logged = true;
if ( $this->session ) {
$this->session->set("user.id", $this->user->id);
}
if ( $this->cookie ) {
$this->cookie->set("user.id", $this->user->id);
}
}
public function logout() : self
{
$this->user->logged = false;
if ( $this->session ) {
$this->session->delete('user.id');
}
@@ -122,10 +117,24 @@ class Authenticate {
$this->cookie->delete('user.id');
}
if ( isset($this->user) ) {
$this->user->logged = false;
}
return $this;
}
public function logUser(?int $id) : ? UserInterface
{
try {
if ($id === null || null === ($entity = $this->user::repository()->loadFromPk($id))) {
return null;
}
}
catch(\Exception $ex) {
return null;
}
$this->user->fromArray($entity);
$this->user->logged = true;
return $this->user;
}
}