larahook/sanctum-refresh-token
Laravel sanctum authentication with refresh token
Install
composer require larahook/sanctum-refresh-token
- Add Trait in
Usermodel class.
use Larahook\SanctumRefreshToken\Trait\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
}
- Add
SanctumRefreshTokenServiceProviderinconfig/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_tokenandrefresh_tokenrefreshTokenPair- unlink current token pair and create newaccess_tokenandrefresh_tokenlogoutTokenPair- 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);
}
}
Related Packages
cline/bearer
Stripe-style typed API tokens with groups, environments, and audit logging for L...
11,958
0
hasinhayder/tyro
Tyro is a complete Authentication, Authorization, and Role & Privilege (RBAC/ACL...
8,117
685
marufsharia/hyro
Hyro is a modular Laravel RBAC ecosystem featuring advanced role-permission mana...
17
0
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v1.0.12 | ^8.0 | ^10.0| | MIT | |
| v1.0.11 | ^8.0 | ^10.0| | MIT | |
| v1.0.10 | ^8.0 | ^10.0| | MIT | |
| v1.0.9 | ^8.0 | ^10.0| | MIT | |
| v1.0.8 | ^8.0 | ^10.0| | MIT | |
| v1.0.7 | ^8.0 | ^10.0| | MIT | |
| v1.0.6 | ^8.0 | ^10.0| | MIT | |
| v1.0.5 | ^8.0 | ^10.0| | MIT | |
| v1.0.4 | ^8.0 | ^10.0| | MIT | |
| v1.0.3 | ^8.0 | ^10.0 | MIT | |
| v1.0.2 | ^8.0 | ^10.0 | MIT | |
| v1.0.1 | ^8.0 | ^10.0 | MIT | |
| v1.0.0 | ^8.0 | ^10.0 | MIT | |
| v0.0.9 | ^7.4| | ^10.0 | MIT |