larahook / sanctum-refresh-token
Sanctum authentication with refresh token
v1.0.7
2024-10-31 08:03 UTC
Requires
- php: ^8.0
- laravel/framework: ^10.0|^11.0
README
Install
composer require larahook/sanctum-refresh-token
- Add Trait in
User
model class.
use Larahook\SanctumRefreshToken\Trait\HasApiTokens; class User extends Authenticatable { use HasApiTokens; }
- Add
SanctumRefreshTokenServiceProvider
inconfig/app.php
'providers' => ServiceProvider::defaultProviders()->merge([ //... EventServiceProvider::class, RouteServiceProvider::class, SanctumRefreshTokenServiceProvider::class, ])->toArray(),
Config
- You can also publish the config file to change implementations
php artisan vendor:publish --provider="Larahook\SanctumRefreshToken\SanctumRefreshTokenServiceProvider" --tag=config
Migration
- Install migrations
php artisan vendor:publish --provider="Larahook\SanctumRefreshToken\SanctumRefreshTokenServiceProvider" --tag=migrations
php artisan migrate
Usage
Add trait AuthTokens
createTokenPair
- createaccess_token
andrefresh_token
refreshTokenPair
- unlink current token pair and create newaccess_token
andrefresh_token
logoutTokenPair
- unlink current token pair
use Larahook\SanctumRefreshToken\Trait\AuthTokens; class SomeClass { use AuthTokens; public function login(string $email, string $password, string $deviceName): array { $user = User::whereEmail($email)->first(); // ...some login pass validation return $this->createTokenPair($user, $deviceName); } /** * @param User $user * * @return array */ public function refresh(User $user): array { return $this->refreshTokenPair($user); } /** * @param User $user * * @return bool */ public function logout(User $user): bool { return $this->logoutTokenPair($user); } }