Skip to content

Commit

Permalink
feat: add fromOAuth method
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Nov 26, 2024
1 parent f0f7355 commit a6369e7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,37 @@ public function updatePassword(string $oldPassword, string $newPassword): bool
return true;
}

/**
* Create a new user from OAuth
*
* @param array $userData User data
*
* @return bool
*/
public function fromOAuth(array $userData): bool
{
$this->checkDbConnection();

Config::setUserCache('oauth-token', $userData['token']);

$user = $this->db->select(Config::get('db.table'))->where($userData['user'])->first();

if (!$user) {
return $this->register($userData['user']);
}

$this->user = new User($user);
return true;
}

/**
* Get saved OAuth token
*/
public function oauthToken()
{
return Config::getUserCache('oauth-token');
}

/**
* Sign a user out
* ---
Expand Down
31 changes: 31 additions & 0 deletions src/Auth/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class Config
'messages.loginPasswordError' => 'Password is incorrect!',
];

/**
* Additional user information for cache
*/
protected static array $userCache = [];

/**
* Set Leaf Auth config
*/
Expand Down Expand Up @@ -68,4 +73,30 @@ public static function get($key = null)

return static::$config;
}

/**
* Set user cache
*/
public static function setUserCache($key, $value): void
{
if (isset($_SESSION)) {
$_SESSION[$key] = $value;
}

static::$userCache[$key] = $value;
}

/**
* Get user cache
*/
public static function getUserCache($key = null)
{
$cache = $_SESSION ?? static::$userCache;

if ($key) {
return $cache[$key] ?? null;
}

return $cache;
}
}

0 comments on commit a6369e7

Please sign in to comment.