百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

laravel-magic-login

> 后端框架
开源

在您的应用程序中轻松添加无密码认证

181 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

在您的应用程序中轻松添加无密码认证

Laravel Magic Login

Easily add passwordless authentication into your application.

Installation

You can install the package via composer:

bash
composer require maize-tech/laravel-magic-login

You can publish the config and migration files and run the migrations with:

bash
php artisan magic-login:install

This is the contents of the published config file:

…

Usage

Basic

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:

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:

php
use App\Models\User;
use Maize\MagicLogin\Facades\MagicLink;

$user = User::firstOrFail();

$magicLink = MagicLink::make(
    authenticatable: $user
);

Email notifications

Send an email notification

Optionally, you may also automatically send an email notification to the given user using the send method:

php
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:

php
use App\Models\User;
use Maize\MagicLogin\Facades\MagicLink;

$user = User::firstOrFail();

$magicLink = MagicLink::make(
    authenticatable: $user,
    notify: true
);

Customize the notification class

When needed, you can customize the email notification. All you have to do is create your own notification and override the default MagicLinkNotification class:

php
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.

Force single link

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.

Magic link generator options

The package offers many useful parameters for the make method to allow you customizing every single magic link:

  • Redirect url
  • Expiration time
  • Route name
  • Authentication guard
  • Logins limit
  • Custom metadata

Redirect url

You can provide a redirect url used after authenticating the user:

php
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.

Expiration time

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:

php
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.

Route name

You can define the route name used to generate the magic link:

php
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.

Authentication guard

You can define the authentication guard used to authenticate the user:

php
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.

Logins limit

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:

php
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.

Custom metadata

Optionally, you can store custom metadata in case you need to perform custom queries.

php
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 0

Testing

bash
composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

  • Enrico De Lazzari
  • All Contributors

License

The MIT License (MIT). Please see License File for more information.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPauthenticationlaravelloginmagic-auth

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架