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;
// ...
}
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');
use Torqie\LaravelPasswordless\Contracts\GeneratesMagicLink;
class MyGenerateMagicLinkAction implements GeneratesMagicLink
{
public function generate(\Illuminate\Contracts\Auth\Authenticatable $authenticatable): string
{
// your logic
}
}
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]);
}
}
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}");
}
}