makkinga / laravel-trusted-devices
将信任设备添加到您的用户模型中
1.3.2
2023-05-08 19:36 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^10.8
- jenssegers/agent: ^2.6
- livewire/livewire: ^2.12
- spatie/laravel-package-tools: ^1.15
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
README
安装
您可以通过composer安装此包
composer require makkinga/laravel-trusted-devices
您可以使用以下命令发布并运行迁移
php artisan vendor:publish --tag="trusted-devices-migrations"
php artisan migrate
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="trusted-devices-config"
这是已发布的配置文件的内容
return [ # Overwrite the auto detection of the guard 'guard' => null, # The layout to use for the views 'layout' => 'layouts.app', # The middleware to use for the routes 'middleware' => ['web', 'auth'], # Automatically trust the first device 'trust_first_device' => true ];
可选地,您可以使用以下命令发布视图
php artisan vendor:publish --tag="trusted-devices-views"
IpInfo
为了利用IpInfo.io每月免费50k请求的速率限制,请将您的API令牌添加到.env文件中
TRUSTED_DEVICES_IPINFO_TOKEN="{your_token}"
使用方法
通过添加HasTrustedDevices
特性,准备您的用户模型,并确保它正在使用Notifiable
特性
use Illuminate\Notifications\Notifiable; use Makkinga\TrustedDevices\Traits\HasTrustedDevices; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable, HasTrustedDevices; }
然后,将信任设备中间件添加到app/Http/Kernel.php
中的$routeMiddleware
use Makkinga\TrustedDevices\Middleware\EnsureDeviceIsTrusted; protected $routeMiddleware = [ [...] 'trusted' => EnsureDeviceIsTrusted::class, ];
现在,您可以在路由和路由组中使用“trusted”中间件,如下所示
Route::middleware(['auth', 'trusted'])->group(function () { // Your routes });
Route::get('/my-route', [MyController::class, 'method'])->name('my-route')->middleware('trusted');
贡献
有关详细信息,请参阅CONTRIBUTING
鸣谢
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。