Skip to content

Instantly share code, notes, and snippets.

@samzzi
Forked from newtonjob/MagicLoginLink.php
Created April 7, 2025 13:23
Show Gist options
  • Save samzzi/a99243c60a301c0f9fa84d5add7ccdec to your computer and use it in GitHub Desktop.
Save samzzi/a99243c60a301c0f9fa84d5add7ccdec to your computer and use it in GitHub Desktop.
Magic login link using Laravel's `Password` broker
<?php
class MagicLoginLinkController extends Controller
{
public function create(Request $request, User $user)
{
$user->notify(new MagicLoginLink(Password::createToken($user)));
return response()->json(['message' => 'Magic login link sent.']);
}
public function store(Request $request, User $user): RedirectResponse
{
if (! Password::tokenExists($user, $request->token)) {
abort(404);
}
Auth::login($user);
Password::deleteToken($user);
return redirect()->intended('dashboard');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment