PHP code example of pushery / email-magic-link-for-laravel
1. Go to this page and download the library: Download pushery/email-magic-link-for-laravel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pushery / email-magic-link-for-laravel example snippets
use EmailMagicLink\Facades\EmailMagicLink;
// A single-use magic link for the default guard.
$link = EmailMagicLink::issueLink($user);
$link->url; // signed, single-use confirmation URL — deliver this verbatim
$link->expiresAt; // Illuminate\Support\Carbon
$link->expiresInMinutes; // e.g. 15 — handy for your own copy
// A one-time code instead.
$code = EmailMagicLink::issueCode($user);
$code->code; // the code to deliver
$code->expiresAt;
$code->expiresInMinutes;
use EmailMagicLink\Contracts\MagicLinkIssuer;
public function __construct(private MagicLinkIssuer $issuer) {}
$link = $issuer->issueLink($user, maxUses: 3); // redeemable three times
use EmailMagicLink\Contracts\TokenStore;
use Illuminate\Support\Facades\URL;
// Mint a single-use token for the user — nothing is sent.
$issued = app(TokenStore::class)->issue($user, config('auth.defaults.guard'), 'link');
// Build a link to YOUR OWN route with the raw token. It is a 256-bit unguessable
// secret; sign the route as well for URL-level expiry and tamper-resistance.
$url = URL::temporarySignedRoute('invoices.download', $issued->record->expires_at, [
'token' => $issued->plaintext,
]);
// deliver $url however you like — email, SMS, chat …
// On your route, authorize by consuming the token — without logging anyone in.
Route::get('/invoices/download', function (Request $request) {
$token = (string) $request->query('token');
$result = app(TokenStore::class)->claimLink($token);
abort_unless($result->successful, 403);
// The claim is atomic and single-use; serve the resource for the token's user.
return Storage::download(invoicePathFor($result->token->user_id));
})->middleware('signed')->name('invoices.download');
use EmailMagicLink\Contracts\ResendGuard;
public function __construct(private ResendGuard $guard) {}
public function resend(Request $request): Response
{
$decision = $this->guard->attempt('challenge:'.$request->user()->id);
if (! $decision->allowed) {
// Seconds until the next send is allowed — render a countdown.
return back()->with('retry_after', $decision->retryAfterSeconds);
}
// …send your mail…
}