makkinga / laravel-trusted-devices
Add trusted devices to your user models
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
Installation
You can install the package via composer:
composer require makkinga/laravel-trusted-devices
You can publish and run the migrations with:
php artisan vendor:publish --tag="trusted-devices-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="trusted-devices-config"
This is the contents of the published config file:
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 ];
Optionally, you can publish the views using
php artisan vendor:publish --tag="trusted-devices-views"
IpInfo
In order to make use of IpInfo.io's free 50k requests per month rate limit, add your API token to your .env:
TRUSTED_DEVICES_IPINFO_TOKEN="{your_token}"
Usage
Prepare your user model by adding the HasTrustedDevices
trait and also make sure it is using the Notifiable
trait:
use Illuminate\Notifications\Notifiable; use Makkinga\TrustedDevices\Traits\HasTrustedDevices; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable, HasTrustedDevices; }
Then add the trusted device middleware to your $routeMiddleware
in app/Http/Kernel.php
:
use Makkinga\TrustedDevices\Middleware\EnsureDeviceIsTrusted; protected $routeMiddleware = [ [...] 'trusted' => EnsureDeviceIsTrusted::class, ];
You can now use the "trusted" middleware on your routes and route groups like this:
Route::middleware(['auth', 'trusted'])->group(function () { // Your routes });
Route::get('/my-route', [MyController::class, 'method'])->name('my-route')->middleware('trusted');
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.