makkinga/laravel-trusted-devices

Add trusted devices to your user models

1.3.2 2023-05-08 19:36 UTC

This package is auto-updated.

Last update: 2024-04-08 21:31:43 UTC


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.