makkinga/laravel-trusted-devices

将信任设备添加到您的用户模型中

1.3.2 2023-05-08 19:36 UTC

This package is auto-updated.

Last update: 2024-09-08 22:19:03 UTC


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)。请参阅许可证文件以获取更多信息。