Skip to content

Commit

Permalink
feat(auth): Update AuthTokens trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
m.utkin committed Mar 27, 2024
1 parent f2ad9c8 commit e21db44
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
19 changes: 19 additions & 0 deletions src/Repository/PersonalAccessTokenRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Larahook\SanctumRefreshToken\Repository;

class PersonalAccessTokenRepository implements PersonalAccessTokenRepositoryInterface
{
/**
* @param int $accessTokenId
* @param int $refreshTokenId
*
* @return bool
*/
public function saveTokenPair(int $accessTokenId, int $refreshTokenId): bool
{
return (bool) config('sanctum-refresh-token.personal_access_token_model')::whereId($accessTokenId)->update([
'refresh_id' => $refreshTokenId,
]);
}
}
8 changes: 8 additions & 0 deletions src/Repository/PersonalAccessTokenRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Larahook\SanctumRefreshToken\Repository;

interface PersonalAccessTokenRepositoryInterface
{
public function saveTokenPair(int $accessTokenId, int $refreshTokenId): bool;
}
9 changes: 6 additions & 3 deletions src/SanctumRefreshTokenServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ class SanctumRefreshTokenServiceProvider extends ServiceProvider
public function register(): void
{
$this->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.
*/
Expand Down Expand Up @@ -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);
}

/**
Expand Down
21 changes: 16 additions & 5 deletions src/Trait/AuthTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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);
}
}

0 comments on commit e21db44

Please sign in to comment.