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

$link = $issuer->issueLink($user, passphrase: 'the-secret-you-shared');

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…
}

'invalid_response' => [
    'via' => 'view', // 'redirect' | 'view' | 'abort' | 'json' | YourResponder::class
    'view' => 'email-magic-link::invalid',
    'redirect_to' => null,
    'abort_status' => 403,
    'error_code' => 'invalid_or_expired',
],

use Illuminate\Support\Facades\Schedule;

Schedule::command('email-magic-link:purge')->daily();

'guard' => 'web',
'guards' => ['admin'],

use EmailMagicLink\Contracts\MagicLinkAuthenticator;

$this->app->bind(MagicLinkAuthenticator::class, MyAuthenticator::class);

final class TurnstileGuard implements CaptchaGuard
{
    public function passes(Request $request): bool
    {
        // Verify the challenge token (e.g. cf-turnstile-response) with the provider.
        return Http::asForm()->post('https://challenges.cloudflare.com/turnstile/v0/siteverify', [
            'secret' => config('services.turnstile.secret'),
            'response' => $request->input('cf-turnstile-response'),
        ])->json('success') === true;
    }
}
bash
php artisan email-magic-link:install
bash
php artisan migrate
bash
php artisan vendor:publish --tag=email-magic-link-config
php artisan vendor:publish --tag=email-magic-link-migrations
php artisan vendor:publish --tag=email-magic-link-views
bash
php artisan queue:work
json
{ "message": "Please wait 30 seconds before requesting another sign-in email.", "error": "resend_throttled" }
bash
php artisan vendor:publish --tag=email-magic-link-lang