PHP code example of haikallfiqih / laravel-magic-auth

1. Go to this page and download the library: Download haikallfiqih/laravel-magic-auth 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/ */

    

haikallfiqih / laravel-magic-auth example snippets


use LaravelLinkAuth\MagicAuth\Facades\MagicAuth;

// Send via email (default)
MagicAuth::sendMagicLink('[email protected]');

// Send via WhatsApp
MagicAuth::sendMagicLink('+1234567890', 'web', [], ['whatsapp']);

// Send via SMS
MagicAuth::sendMagicLink('+1234567890', 'web', [], ['sms']);

// With custom attributes for new users
MagicAuth::sendMagicLink('[email protected]', 'web', [
    'name' => 'John Doe',
    'company_id' => 1
]);

use LaravelLinkAuth\MagicAuth\Http\Controllers\MagicAuthController;

Route::post('/magic-link', [MagicAuthController::class, 'sendMagicLink'])
    ->name('magic-auth.send');

Route::get('/auth/verify', [MagicAuthController::class, 'verify'])
    ->name('magic-auth.verify')
    ->middleware('signed');

use LaravelLinkAuth\MagicAuth\Facades\Events;
use LaravelLinkAuth\MagicAuth\Events\MagicAuthEvents;

// Before generating a magic link
Events::listen(MagicAuthEvents::GENERATING, function ($notifiable, $guard, $attributes) {
    // Validate or modify attributes
});

// After sending a magic link
Events::listen(MagicAuthEvents::SENT, function ($notifiable, $guard, $linkId) {
    // Log or track magic link usage
});

// When verification succeeds
Events::listen(MagicAuthEvents::VERIFICATION_COMPLETED, function ($user, $guard) {
    // Handle successful login
});

return [
    // Link expiration time in minutes
    'expires' => 15,

    // Authentication guards configuration
    'guards' => [
        'web' => [
            'provider' => 'users',
            'model' => \App\Models\User::class,
            'redirect_on_success' => '/dashboard',
        ],
    ],

    // Rate limiting settings
    'throttle' => [
        'max_attempts' => 5,
        'decay_minutes' => 10,
    ],

    // Available notification channels
    'channels' => [
        'default' => ['mail'],
        'available' => ['mail', 'whatsapp', 'sms'],
    ],
];
bash
php artisan vendor:publish --provider="LaravelLinkAuth\MagicAuth\MagicAuthServiceProvider"
bash
php artisan migrate