From e21db44e30f4d3695ffd509d000fd6d59ece58f6 Mon Sep 17 00:00:00 2001 From: "m.utkin" Date: Wed, 27 Mar 2024 12:11:20 +0300 Subject: [PATCH] feat(auth): Update AuthTokens trait. --- .../PersonalAccessTokenRepository.php | 19 +++++++++++++++++ ...PersonalAccessTokenRepositoryInterface.php | 8 +++++++ src/SanctumRefreshTokenServiceProvider.php | 9 +++++--- src/Trait/AuthTokens.php | 21 ++++++++++++++----- 4 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 src/Repository/PersonalAccessTokenRepository.php create mode 100644 src/Repository/PersonalAccessTokenRepositoryInterface.php diff --git a/src/Repository/PersonalAccessTokenRepository.php b/src/Repository/PersonalAccessTokenRepository.php new file mode 100644 index 0000000..5e5ffd9 --- /dev/null +++ b/src/Repository/PersonalAccessTokenRepository.php @@ -0,0 +1,19 @@ +update([ + 'refresh_id' => $refreshTokenId, + ]); + } +} \ No newline at end of file diff --git a/src/Repository/PersonalAccessTokenRepositoryInterface.php b/src/Repository/PersonalAccessTokenRepositoryInterface.php new file mode 100644 index 0000000..be6d523 --- /dev/null +++ b/src/Repository/PersonalAccessTokenRepositoryInterface.php @@ -0,0 +1,8 @@ +mergeConfigFrom(__DIR__.'/../config/sanctum-refresh-token.php', 'sanctum-refresh-token'); + + # Repositories + $this->app->bind(\Larahook\SanctumRefreshToken\Repository\PersonalAccessTokenRepositoryInterface::class, \Larahook\SanctumRefreshToken\Repository\PersonalAccessTokenRepository::class); } - + /** * Bootstrap services. */ @@ -61,8 +64,8 @@ private function isTokenAbilityValid(PersonalAccessToken $token): bool } return collect($routeNames)->contains(Route::currentRouteName()) ? - $this->isRefreshTokenValid($token) : - $this->isAuthTokenValid($token); + $this->isRefreshTokenValid($token) : + $this->isAuthTokenValid($token); } /** diff --git a/src/Trait/AuthTokens.php b/src/Trait/AuthTokens.php index 4759e72..1008f6c 100644 --- a/src/Trait/AuthTokens.php +++ b/src/Trait/AuthTokens.php @@ -5,10 +5,14 @@ use App\Models\User; use Carbon\Carbon; use Illuminate\Support\Str; +use Larahook\SanctumRefreshToken\Repository\PersonalAccessTokenRepositoryInterface; use Laravel\Sanctum\NewAccessToken; trait AuthTokens { + /* @var $personalAccessTokenRepository PersonalAccessTokenRepositoryInterface */ + private PersonalAccessTokenRepositoryInterface $personalAccessTokenRepository; + /** * @param User $user * @param string $deviceName @@ -23,14 +27,15 @@ public function createTokens( ?Carbon $accessTokenExpiresAt = null, ?Carbon $refreshTokenExpiresAt = null, ): array { + $this->init(); + $accessToken = $user->createAuthToken($deviceName, $accessTokenExpiresAt); $refreshToken = $user->createRefreshToken($deviceName, $refreshTokenExpiresAt); - $accessTokenId = $accessToken->accessToken->getAttribute('id'); - $refreshTokenId = $refreshToken->accessToken->getAttribute('id'); - - config('sanctum-refresh-token.personal_access_token_model')::whereId($accessTokenId) - ->update(['refresh_id' => $refreshTokenId,]); + $this->personalAccessTokenRepository->saveTokenPair( + $accessToken->accessToken->getAttribute('id'), + $refreshToken->accessToken->getAttribute('id'), + ); return [ 'access_token' => $accessToken->plainTextToken, @@ -39,4 +44,10 @@ public function createTokens( 'refresh_token_expiration' => $refreshToken->accessToken->expires_at ?? null, ]; } + + private function init() + { + /* @var $personalAccessTokenRepository PersonalAccessTokenRepositoryInterface */ + $this->personalAccessTokenRepository = app()->make(PersonalAccessTokenRepositoryInterface::class); + } }