在您的应用程序中轻松添加无密码认证
Easily add passwordless authentication into your application.
You can install the package via composer:
composer require maize-tech/laravel-magic-loginYou can publish the config and migration files and run the migrations with:
php artisan magic-login:installThis is the contents of the published config file:
…To use the package, all you have to do is include the magic link route in your routes file.
By default, you should include it under routes/web.php:
use Maize\MagicLogin\Facades\MagicLink;
MagicLink::route();That's it!
Once done, you can already generate an invitation link to any model extending the Authenticatable class using the make method:
use App\Models\User;
use Maize\MagicLogin\Facades\MagicLink;
$user = User::firstOrFail();
$magicLink = MagicLink::make(
authenticatable: $user
);Optionally, you may also automatically send an email notification to the given user using the send method:
use App\Models\User;
use Maize\MagicLogin\Facades\MagicLink;
$user = User::firstOrFail();
$magicLink = MagicLink::send(
authenticatable: $user
);which is equals to using the make method with the notify parameter set to true:
use App\Models\User;
use Maize\MagicLogin\Facades\MagicLink;
$user = User::firstOrFail();
$magicLink = MagicLink::make(
authenticatable: $user,
notify: true
);When needed, you can customize the email notification.
All you have to do is create your own notification and override the default MagicLinkNotification class:
use Illuminate\Notifications\Messages\MailMessage;
use Maize\MagicLogin\Notifications\MagicLinkNotification as BaseMagicLinkNotification;
class MagicLinkNotification extends BaseMagicLinkNotification
{
public function toMail($notifiable): MailMessage
{
return (new MailMessage)
->line(__('This is my custom magic link notification message'))
->action(__('Join now'), $this->uri);
}
}Finally, you can update the notification attribute under config/magic-login.php with the new class path.
When enabled, users can only have one valid link at a time. This means that when a new link is created, all previously created links are invalidated.
To enable this option, you can set the force_single attribute under config/magic-login.php to true.
The package offers many useful parameters for the make method to allow you customizing every single magic link:
You can provide a redirect url used after authenticating the user:
use App\Models\User;
use Maize\MagicLogin\Facades\MagicLink;
$user = User::firstOrFail();
$magicLink = MagicLink::send(
authenticatable: $user,
redirectUrl: 'yourapplication.test/your-path'
);When not provided, the default value defined in redirect_url attribute under config/magic-login.php will be used.
You can define the amount of time before a magic link expires by providing a carbon instance or an integer with the amount of minutes:
use App\Models\User;
use Maize\MagicLogin\Facades\MagicLink;
$user = User::firstOrFail();
$magicLink = MagicLink::send(
authenticatable: $user,
expiration: now()->addDays(10), // the link will expire in 10 days
);
$magicLink = MagicLink::send(
authenticatable: $user,
expiration: 60, // the link will expire in 1 hour (60 minutes)
);When not provided, the default value defined in expiration attribute under config/magic-login.php will be used.
You can define the route name used to generate the magic link:
use App\Models\User;
use Maize\MagicLogin\Facades\MagicLink;
$user = User::firstOrFail();
$magicLink = MagicLink::send(
authenticatable: $user,
routeName: 'magic-link',
);When not provided, the default value defined in route.name attribute under config/magic-login.php will be used.
You can define the authentication guard used to authenticate the user:
use App\Models\User;
use Maize\MagicLogin\Facades\MagicLink;
$user = User::firstOrFail();
$magicLink = MagicLink::send(
authenticatable: $user,
guard: 'api' // the 'api' auth guard will be used
);When not provided, the default value defined in guard attribute under config/magic-login.php will be used.
You can define the amount of times a single link can be used before expiring. The value can be either -1, which lets the user login indefinitely, or any number greater than or equal to 1:
use App\Models\User;
use Maize\MagicLogin\Facades\MagicLink;
$user = User::firstOrFail();
$magicLink = MagicLink::send(
authenticatable: $user,
loginsLimit: 5 // the link can be used 5 times at max
);
$magicLink = MagicLink::send(
authenticatable: $user,
loginsLimit: -1 // the link can be used an infinite amount of times
);When not provided, the default value defined in logins_limit attribute under config/magic-login.php will be used.
Optionally, you can store custom metadata in case you need to perform custom queries.
use App\Models\User;
use Maize\MagicLogin\Facades\MagicLink;
use Maize\MagicLogin\Models\MagicLogin;
$user = User::firstOrFail();
$magicLink = MagicLink::send(
authenticatable: $user,
metadata: ['test' => true]
);
MagicLogin::query()
->whereJsonContains('metadata->test', true)
->count(); // returns 1
MagicLogin::query()
->whereJsonContains('metadata->test', false)
->count(); // returns 0composer testPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.
暂无开放 Issues,或尚未同步最近议题。