laravel-magic-link-auth maintained by snb4crazy
Description
Magic link authentication package for Laravel APIs with Sanctum support.
Last update
2026/10/28 01:06
(dev-BugFix/IdentityBoundExchange)
Downloads
0
Tags
Laravel Magic Link Auth
Reusable Laravel package for passwordless "magic link" login, designed for API backends (including Laravel Sanctum token issuing).
Integration guides
Features
POST /api/auth/magic-link/requestto request a one-time login linkPOST /api/auth/magic-link/exchangeto exchange link token for authenticated payload- One-time token consumption with expiry window
- Enumeration-safe request response (same success message for known/unknown emails)
- Configurable route prefix, middleware, TTL, lookup model/column, and frontend/deep-link URL
- Optional verified-email requirement and optional existing-token revocation
Installation
composer require snb4crazy/laravel-magic-link-auth
php artisan migrate
Publish package config when you need customization:
php artisan vendor:publish --tag=magic-link-auth-config
Configuration
Default file: config/magic-link-auth.php
MAGIC_LINK_FRONTEND_URL=myapp://auth/magic-link
MAGIC_LINK_TTL_MINUTES=15
MAGIC_LINK_EXCHANGE_CANDIDATE_LIMIT=5
MAGIC_LINK_REQUIRE_VERIFIED_EMAIL=false
MAGIC_LINK_ISSUE_SANCTUM_TOKEN=true
MAGIC_LINK_REVOKE_EXISTING_TOKENS=false
Important options
MAGIC_LINK_FRONTEND_URL: Link base URL included in email (email+tokenquery params are appended).MAGIC_LINK_EXCHANGE_CANDIDATE_LIMIT: Limits how many latest active tokens are checked on exchange (performance tuning).MAGIC_LINK_ISSUE_SANCTUM_TOKEN: Whentrue, the package returnstoken+token_typeif user model supportscreateToken.MAGIC_LINK_REQUIRE_VERIFIED_EMAIL: Whentrue, unverified users receive403during exchange.
API Contract
Request magic link
POST /api/auth/magic-link/request
{
"email": "user@example.com"
}
Always returns:
{
"message": "If that email is registered, a magic link has been sent."
}
Exchange magic link
POST /api/auth/magic-link/exchange
{
"email": "user@example.com",
"token": "64-char-token-from-link"
}
Session mode response:
{
"user": { "...": "..." },
"message": "Logged in successfully."
}
Token mode response:
{
"user": { "...": "..." },
"token": "plain-text-api-token",
"token_type": "Bearer"
}
Integration Notes
Laravel backend integration
- Ensure your user model is configured in
auth.providers.users.model. - If using token mode, your user model must expose
createToken(...)(for example via Sanctum'sHasApiTokens). - Named routes are available:
magic-link-auth.requestandmagic-link-auth.exchange.
Mobile/Flutter deep-link integration
Set MAGIC_LINK_FRONTEND_URL to your app deep link (for example myapp://auth/magic-link). The package appends email and token, which your app can read and submit to /exchange.
Development and testing
Run the package test suite:
composer test