albetnov/sanctum-refresh
Composer 安装命令:
composer require albetnov/sanctum-refresh
包简介
This package extends sanctum ability to be able to provide refresh token as well
README 文档
README
Minimal and flexible package to extend Sanctum to have refresh token as well.
Installation
You can install the package via composer:
composer require albetnov/sanctum-refresh
Then you'll need to push and run the migration with:
php artisan vendor:publish --tag="sanctum-refresh-migrations"
php artisan migrate
You can also publish the config file with:
php artisan vendor:publish --tag="sanctum-refresh-config"
This is the contents of the published config file:
return [ /** * Set the fallback expiration time of both tokens * Time in minutes. */ 'expiration' => [ // set the fallback of access token expiration 'access_token' => 2, // 2 minutes, // set the fallback of refresh token expiration 'refresh_token' => 30, // 30 minutes ], ];
Quick Start
Creating token
<?php namespace App\Http\Controllers; use Albet\SanctumRefresh\Services\TokenIssuer; class TokenController { function newToken() { $token = TokenIssuer::issue($request->user(), guard: 'api'); return response()->json([ 'message' => 'Token generated successfully!', 'data' => $token->toArray(), ]); } }
Response schema:
{
"message": "Token generated successfully!",
"data": {
"access_token": "[string]",
"access_token_expires_at": "[Y-m-d H:i:s]",
"refresh_token": "[string]",
"refresh_token_expires_at": "[Y-m-d H:i:s]"
}
}
Refresh Token Middleware (optional, if you want to customize error based on expired, invalid format, etc)
Create the Middleware
<?php // (...) use Albet\SanctumRefresh\Helpers; use Albet\SanctumRefresh\Exceptions\SanctumRefreshException; class TokenMiddleware { public function handle(Request $request, \Closure $next): Response { try { Helpers::getRefreshToken( $request->get('refresh_token', '') // adjust to your liking, either from Query Parameter, Body, or Header. ); return $next($request); } catch (SanctumRefreshException $e) { // handle tags of SanctumRefreshException return response()->json([ 'error' => 'Refresh token invalid' ], 400); } } }
Applying your middleware to your routes
<?php // imports... Route::post('refresh-token', [TokenController::class, 'refreshToken'])->middleware(TokenMiddleware::class);
Handling the refresh token creation
<?php use Albet\SanctumRefresh\Services\TokenIssuer; class TokenController { public function refreshToken(Request $request) { $newToken = TokenIssuer::refreshToken($request->get('refresh-token', '')); if(!$newToken) { return response()->json([ 'error' => 'Refresh token not valid', ], 400); } return response()->json([ 'message' => 'New token created', 'data' => $newToken->toArray(), ]); } }
Pruning Token
Register prune:token on your commands Kernel.php, you can run it as cron job:
Schedule::command('prune:token')->daily();
Testing
Run the tests:
composer test
Figure out the code coverage:
composer test-coverage
Changelog
Please see Changelog for more information.
Contributing
You are free to contribute to this project.
Credits
License
The MIT License (MIT). Please see License File for more information.
albetnov/sanctum-refresh 适用场景与选型建议
albetnov/sanctum-refresh 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.63k 次下载、GitHub Stars 达 21, 最近一次更新时间为 2023 年 01 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「refresh-token」 「albetnov」 「sanctum-refresh」 「sanctum-refresh-token」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 albetnov/sanctum-refresh 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 albetnov/sanctum-refresh 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 albetnov/sanctum-refresh 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Tokenable provides multi-model and multi-platform authentication with refresh tokens for APIs, SPAs, and SSR.
A package to help you implement refresh token mechanism in your laravel application
Alfabank REST API integration
Minimal-dependency JWT auth for first-party Laravel apps (unofficial; not affiliated with the Laravel team): short access tokens, opaque rotating refresh tokens, reuse detection, grace window, and instant denylist revocation.
Provides token-based authentication system for Laravel REST APIs
OAuth2/JWT toolbox with multi-population support
统计信息
- 总下载量: 1.63k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 21
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-01-19