You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.3 KiB
46 lines
1.3 KiB
<?php
|
|
|
|
namespace authkit2\Observers;
|
|
use authkit2\Authkit2;
|
|
use authkit2\Models\Token;
|
|
|
|
class UserObserver
|
|
{
|
|
/*'retrieved', 'creating', 'created', 'updating', 'updated',
|
|
'saving', 'saved', 'restoring', 'restored', 'replicating',
|
|
'deleting', 'deleted', 'forceDeleted',*/
|
|
|
|
protected static $token_cache = [];
|
|
|
|
public function retrieved($user)
|
|
{
|
|
// Find the token + refresh token for the user
|
|
$token = Token::where('user_id', $user->{$user->getAuthIdentifierName()})->first();
|
|
|
|
// Create a token object
|
|
$user->authkit = Authkit2::get_token($token->access_token, $token->refresh_token);
|
|
|
|
// Set a refresh callback on the token -- when it's been refreshed,
|
|
// save the new tokens.
|
|
$user->authkit->setRefreshCallback(function($oidc_token) use ($token) {
|
|
$token->access_token = $oidc_token->getAccessToken();
|
|
$token->refresh_token = $oidc_token->getRefreshToken();
|
|
$token->save();
|
|
});
|
|
}
|
|
|
|
public function saving($user)
|
|
{
|
|
if (isset($user->authkit))
|
|
static::$token_cache[$user->{$user->getAuthIdentifierName()}] = $user->authkit;
|
|
unset($user->authkit);
|
|
}
|
|
|
|
public function saved($user)
|
|
{
|
|
$user_id = $user->{$user->getAuthIdentifierName()};
|
|
if (isset(static::$token_cache[$user_id]))
|
|
$user->authkit = static::$token_cache[$user_id];
|
|
|
|
}
|
|
}
|
|
|