PHP code example of torqie / laravel-passwordless

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

    

torqie / laravel-passwordless example snippets


use Torqie\LaravelPasswordless\Traits\HasPasswordlessAuth;

class User extends Authenticatable
{
    use HasPasswordlessAuth;
    // ...
}

// config/passwordless.php
'user_model' => App\Models\User::class,

use Torqie\LaravelPasswordless\Facades\LaravelPasswordless;

// Send a magic link — returns the signed URL
$url = LaravelPasswordless::for($user)->sendMagicLink();

// Send a login code — returns the plain-text code
$code = LaravelPasswordless::for($user)->sendLoginCode();

// All tokens associated with the user
$user->passwordlessTokens;

// Only valid (non-expired, non-used) tokens
$user->validPasswordlessTokens;

// Invalidate all unused tokens (optional: scope to one type)
$user->invalidatePasswordlessTokens();
$user->invalidatePasswordlessTokens('magic_link');
$user->invalidatePasswordlessTokens('login_code');

'type' => 'both', // 'magic_link' | 'login_code' | 'both'

'ttl' => 15,

'code' => [
    'length'  => 6,            // Number of characters in the code
    'charset' => '0123456789', // Characters to draw from
],

'guard' => 'web',

'user_model' => \App\Models\User::class,

'redirects' => [
    'after_login'   => '/dashboard',
    'invalid_token' => '/login',
],

'routes' => [
    'prefix'     => 'auth',
    'middleware' => ['web'],
],

'routes' => [
    'prefix'     => 'login',
    'middleware' => ['web'],
],

'views' => [
    'magic_link_request' => null,  // GET /auth/magic-link
    'magic_link_sent'    => null,  // after POST /auth/magic-link
    'magic_link_email'   => null,  // email notification
    'login_code_request' => null,  // GET /auth/code
    'login_code_verify'  => null,  // GET /auth/code/verify
    'login_code_email'   => null,  // email notification
],

'views' => [
    'magic_link_email' => 'emails.auth.magic-link',
],

'inertia' => true,

'components' => [
    'magic_link_request' => 'Auth/MagicLinkRequest',
    'magic_link_sent'    => 'Auth/MagicLinkSent',
    'login_code_request' => 'Auth/LoginCodeRequest',
    'login_code_verify'  => 'Auth/LoginCodeVerify',
],

'actions' => [
    'generate_magic_link'     => \App\Auth\MyGenerateMagicLinkAction::class,
    'authenticate_magic_link' => \Torqie\LaravelPasswordless\Actions\AuthenticateViaMagicLinkAction::class,
    'generate_login_code'     => \Torqie\LaravelPasswordless\Actions\GenerateLoginCodeAction::class,
    'authenticate_login_code' => \Torqie\LaravelPasswordless\Actions\AuthenticateViaLoginCodeAction::class,
    'resolve_user'            => \Torqie\LaravelPasswordless\Actions\ResolveUserForSendAction::class,
],

use Torqie\LaravelPasswordless\Contracts\GeneratesMagicLink;

class MyGenerateMagicLinkAction implements GeneratesMagicLink
{
    public function generate(\Illuminate\Contracts\Auth\Authenticatable $authenticatable): string
    {
        // your logic
    }
}

'actions' => [
    'generate_magic_link' => \App\Auth\MyGenerateMagicLinkAction::class,
],

namespace App\Auth;

use Illuminate\Contracts\Auth\Authenticatable;
use Torqie\LaravelPasswordless\Actions\ResolveUserForSendAction;
use App\Models\User;

class RegisterOnSendResolver extends ResolveUserForSendAction
{
    public function handle(string $email, string $ip): ?Authenticatable
    {
        // Keeps the send throttling from the parent action.
        if ($user = parent::handle($email, $ip)) {
            return $user;
        }

        return User::create(['email' => $email]);
    }
}

'actions' => [
    'resolve_user' => \App\Auth\RegisterOnSendResolver::class,
],

use Torqie\LaravelPasswordless\Events\UserAuthenticatedPasswordlessly;

class MarkEmailVerified
{
    public function handle(UserAuthenticatedPasswordlessly $event): void
    {
        $event->authenticatable->forceFill(['email_verified_at' => now()])->save();
    }
}

use Torqie\LaravelPasswordless\Events\UserAuthenticatedPasswordlessly;

class LogPasswordlessLogin
{
    public function handle(UserAuthenticatedPasswordlessly $event): void
    {
        activity()
            ->causedBy($event->authenticatable)
            ->log("Logged in via {$event->type}");
    }
}

Route::get('/my-route/{token}', MyController::class)->middleware('passwordless.signed');

Schedule::command('passwordless:purge')->daily();
bash
php artisan passwordless:install
bash
php artisan passwordless:install --force
bash
php artisan vendor:publish --tag="laravel-passwordless-views"
# → resources/views/vendor/laravel-passwordless/
bash
php artisan passwordless:install-inertia
bash
php artisan passwordless:install
bash
php artisan passwordless:install --force
bash
# Auto-detect from package.json
php artisan passwordless:install-inertia

# Explicit framework
php artisan passwordless:install-inertia --framework=vue
php artisan passwordless:install-inertia --framework=react
php artisan passwordless:install-inertia --framework=svelte

# Overwrite existing stubs
php artisan passwordless:install-inertia --force