cesargb/laravel-magiclink
Laravel package for passwordless authentication: generate signed, temporary magic links tied to a user, validate them, and enforce usage limits or expiration. Offers a simple API for creating and consuming links, with middleware support for login and verification flows.
Install the package
composer require cesargb/laravel-magiclink
Publish and run migrations
php artisan vendor:publish --provider="CesarGB\MagicLink\MagicLinkServiceProvider"
php artisan migrate
This creates the magic_links table and publishes config/magiclink.php.
Add the trait to your authenticatable model (usually User):
use CesarGB\MagicLink\Traits\MagicLinkable;
class User extends Authenticatable
{
use MagicLinkable;
}
Generate your first magic link:
$link = $user->createMagicLink();
// $link->url is the signed URL to send to the user
Protect a route with the middleware (required since v2.29.0):
Route::get('/magic-login', function () {
return 'You are logged in as '.auth()->user()->email;
})->middleware(['web', 'magiclink']);
The middleware validates the token, logs the user in, and redirects or continues.
Where to look first
config/magiclink.php – expiration, max uses, redirect behaviour, guard selection.CesarGB\MagicLink\MagicLink facade – main API for creating and managing links.MagicLinkable trait – model-level helper methods.magiclink middleware – enforces token validation and authentication.First use case
Passwordless login: generate a link for a user and email it. When the user clicks it, the middleware authenticates them and redirects to your dashboard.
Passwordless login flow
$link = $user->createMagicLink();
Mail::to($user)->send(new MagicLoginMail($link->url));
magiclink middleware. After authentication, use the configured redirect (on_success_redirect) or return a custom response.Private content access
$link = $user->createMagicLink([
'redirect' => route('private.document', $document),
]);
magiclink. The middleware authenticates the user and then redirects to the intended URL.Customising the link lifecycle
$link = $user->createMagicLink(['expires_in' => 60, 'max_uses' => 1]);
MagicLink::create($user, $options) for a fluent alternative.$link->delete() or by pruning the database.Integration tips
MagicLinkCreated, MagicLinkUsed) to log or audit access.guard option in config or per link so the middleware logs the user into the correct guard.Custom actions and extension points
on_success_redirect config or by adding your own middleware after magiclink.CesarGB\MagicLink\Contracts).MagicLink model directly for advanced queries (e.g. listing active links for a user).MagiclinkMiddleware is deprecated. Always attach magiclink to the route that consumes the link.APP_URL is correct in every environment, otherwise links will point to the wrong domain.max_uses explicitly, but remember to prune expired tokens.php artisan magiclink:prune (or equivalent) to avoid table bloat, or write a model cleanup job.expires_in is minutes, not seconds. Double-check values when testing.on_failure_redirect to send users to a friendly error page instead of a 403.magic_links table for the token, confirm the link has not expired or been used, and verify the middleware is applied. Logs from the middleware usually indicate why validation failed.guard option so the middleware logs into the right guard.MagicLink model or action classes only if you need deep customisation.How can I help you explore Laravel packages today?