mayoz/laravel-tokens
Composer 安装命令:
composer require mayoz/laravel-tokens
包简介
Multi-token support for Laravel Auth.
关键字:
README 文档
README
Introduction
This package provides a simple solution for give multiple token to your application's users. This solution is similar to Laravel's TokenGuard class.
This package is for Laravel 5.5 and above.
Installation
You can install the package via composer using:
composer require mayoz/laravel-tokens
The service provider will automatically get registered for Laravel 5.5 and above versions. If you want you can add the service provider in config/app.php file:
'providers' => [ // ... Mayoz\Token\TokenServiceProvider::class, ];
If you are going to make changes the default migration, you can publish the migration file with:
php artisan vendor:publish --provider="Mayoz\Token\TokenServiceProvider" --tag="laravel-tokens-migrations"
Then, you can create the tokens table by running the migrations:
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --provider="Mayoz\Token\TokenServiceProvider" --tag="laravel-tokens-config"
If you need you are free to change your config file.
Implementation
After installation, you can implement the new feature for your application.
Add the Mayoz\Token\HasToken trait to your App\User model. This trait will provide a few helper methods to your model which allow you to inspect the authenticated user's tokens:
<?php namespace App; use Mayoz\Token\HasToken; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use HasToken, Notifiable; // ... }
And finally, you will add the new guard to your application. Open the config/auth.php file and apply following changes:
'guards' => [ // ... 'api' => [ 'driver' => 'multi-token', 'provider' => 'tokens', ], ], 'providers' => [ // ... 'tokens' => [ 'driver' => 'eloquent', 'model' => Mayoz\Token\Token::class, ], ],
Congratulations!
Usage
When you need it (after login or any actions later), use the helper function to create a new token.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class TokenController { public function __invoke(Request $request) { $token = $request->user()->generateToken(); return $token; } }
By default tokens never expire if you do not pass the lifetime when generation. For define expiration, you can pass the time period parameter (in minutes) to generateToken method.
Generate a new token of 10 minutes life with:
$token = $request->user()->generateToken(10);
The token are not refreshed, token will die when expired. The authentication attempts with expired token will fail.
The authentication process is similar to that of the standard Laravel api_token flows:
The token guard is looking for the token:
- Firstly looking the URL for parameter
?api_token=XXX - If not exists token, looking the header for
Authorization: Bearer XXX
Finally, if you need the current token model information underlying the authentication process, you can use the token method.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController
{
public function __construct()
{
$this->middleware('auth:api');
}
public function __invoke(Request $request)
{
return [
'user' => $request->user(),
'token' => $request->token(),
];
}
}
Token Generator
By default, the generated token is a string of random 36 chars. If you want to create more meaningful (such as uuid4) tokens, you are free to change the generator method.
Let's make change to generate of uuid4 string. Open the app/Providers/AuthServiceProvider file and apply the additions:
<?php namespace App\Providers; use Mayoz\Token\Generator; // ... class AuthServiceProvider extends ServiceProvider { // ... /** * Register any authentication / authorization services. * * @return void */ public function boot() { // ... Generator::extend(function () { return \Ramsey\Uuid\Uuid::uuid4()->toString(); }); } }
If there is no ramsey/uuid package in your application, you can install with:
composer require ramsey/uuid
Cheers.
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
If you discover any security related issues, please create a new issue with using the "Bug" label. All security vulnerabilities will be promptly addressed.
License
The MIT License (MIT). Please see License File for more information.
mayoz/laravel-tokens 适用场景与选型建议
mayoz/laravel-tokens 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.33k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 02 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「auth」 「laravel」 「multiple token」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mayoz/laravel-tokens 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mayoz/laravel-tokens 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mayoz/laravel-tokens 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Extensions for laravel's database abstraction layer
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
Extension of Magento2 Sales Rule to allow multiple coupon usage.
Email Toolkit Plugin for CakePHP
统计信息
- 总下载量: 13.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-02-24