Two-factor setup
Turning on two-factor authentication in three steps a stepper keeps in view: scan a QR code (or type the setup key), prove it worked with a code from the app, then save the recovery codes. Each step is its own request, so it works with no script, and the wiring below uses Laravel Fortify.


A block, not a component: copy the view below into your app. It assumes the base layout, which loads Alpine and the package's script.
What it shows
-
Scan: the QR code your server draws, in a white square in both themes (not every camera app reads a light-on-dark code), named for a screen reader by its
role="img"box; and the setup key, grouped in fours to read and copied whole by a copy button. The picture above shows a stand-in that encodes nothing. -
Enter a code: the OTP input, which submits itself when the sixth digit lands, with Back and Verify. A wrong code comes back under the boxes:

-
Save recovery codes: the codes in a monospace list, Copy codes, Download as text (a
data:link withdownload, so no route and no script), and "I have saved these codes", which Finish waits for:
-
Progress is said three ways: the stepper's current step (
aria-current="step"), the step heading ("Step 2 of 3: Enter a code") and the page title. Scan links back from step 2; after the code is confirmed there is no going back to a secret that is already set.
Built from
- Page header: the title, the line under it, the way back to Security
- Panel: the card
- Stepper: the three steps
- Copy button: the setup key and the recovery codes
- OTP input: the code from the app
- Checkbox: "I have saved these codes"
- Button: Continue, Back, Verify, Download as text, Finish
The markup
resources/views/settings/two-factor.blade.php
{{--
Turning on two-factor authentication, one step per request: $step is
scan, confirm or codes. The controller passes $qrSvg and $setupKey (from
Fortify's TwoFactorAuthenticatable) for the first, and $recoveryCodes for
the last.
--}}
@php
$steps = ['scan' => 'Scan the code', 'confirm' => 'Enter a code', 'codes' => 'Save recovery codes'];
$position = array_search($step, array_keys($steps)) + 1;
$status = fn (int $n) => $n < $position ? 'complete' : ($n === $position ? 'current' : 'upcoming');
@endphp
<x-layouts.base :title="'Two-factor authentication, step '.$position.' of 3'">
<main class="mx-auto flex max-w-3xl flex-col gap-6 px-4 py-10 sm:py-16">
<x-ds::page-header
title="Turn on two-factor authentication"
description="After your password, Pebblework will ask for a code from an app on your phone."
:backHref="route('settings.security')"
backLabel="Security"
/>
<x-ds::panel body="plain">
<x-ds::stepper label="Setup progress">
{{-- Back to the code from step 2 only: once confirmed, the secret is set. --}}
<x-ds::step title="Scan the code" :status="$status(1)" :href="$step === 'confirm' ? route('two-factor.setup', ['step' => 'scan']) : null" />
<x-ds::step title="Enter a code" :status="$status(2)" />
<x-ds::step title="Save recovery codes" :status="$status(3)" />
</x-ds::stepper>
<h2 class="mt-8 text-title text-fg">Step {{ $position }} of 3: {{ $steps[$step] }}</h2>
@if ($step === 'scan')
<p class="mt-1 text-body text-fg-muted">
Open your authenticator app, add an account, and scan this code with your phone's camera.
</p>
<div class="mt-6 flex flex-col gap-6 sm:flex-row sm:items-start">
{{-- White in both themes: not every camera app reads a light-on-dark code. --}}
<div role="img" aria-label="QR code for adding Pebblework to an authenticator app" class="size-44 shrink-0 rounded-control border border-border bg-white p-3 *:size-full">
{!! $qrSvg !!}
</div>
<div class="min-w-0">
<p class="text-body font-medium text-fg">Can't scan it?</p>
<p class="mt-1 text-body text-fg-muted">Choose "Enter a setup key" in the app and type this key:</p>
<div class="mt-3 flex flex-wrap items-center gap-3">
<code id="setup-key" class="rounded-control bg-surface-sunken px-3 py-2 font-mono text-body break-all text-fg">{{ trim(chunk_split($setupKey, 4, ' ')) }}</code>
<x-ds::copy-button :value="$setupKey" label="Copy key" size="sm" />
</div>
</div>
</div>
<div class="mt-8 flex justify-end border-t border-divider pt-5">
<x-ds::button :href="route('two-factor.setup', ['step' => 'confirm'])">Continue</x-ds::button>
</div>
@elseif ($step === 'confirm')
<p class="mt-1 text-body text-fg-muted">Type the six-digit code your app shows for Pebblework.</p>
<form method="POST" action="{{ route('two-factor.confirm') }}" class="mt-6">
@csrf
<x-ds::otp-input
name="code"
label="Code from your app"
help="The code changes every 30 seconds. Any current one works."
:error="$errors->confirmTwoFactorAuthentication->first('code')"
autofocus
@complete="$el.form.requestSubmit()"
/>
<div class="mt-8 flex justify-between gap-3 border-t border-divider pt-5">
<x-ds::button variant="secondary" :href="route('two-factor.setup', ['step' => 'scan'])">Back</x-ds::button>
<x-ds::button type="submit">Verify</x-ds::button>
</div>
</form>
@else
<p class="mt-1 text-body text-fg-muted">
If you lose your phone, each of these codes signs you in once. Keep them somewhere other than this computer.
</p>
<ul id="recovery-codes" aria-label="Recovery codes" class="mt-5 grid grid-cols-1 gap-x-8 gap-y-1 rounded-control bg-surface-sunken px-4 py-3 font-mono text-body text-fg sm:grid-cols-2">
@foreach ($recoveryCodes as $code)
<li>{{ $code }}</li>
@endforeach
</ul>
<div class="mt-3 flex flex-wrap gap-2">
<x-ds::copy-button :value="implode(PHP_EOL, $recoveryCodes)" label="Copy codes" size="sm" />
<x-ds::button
variant="secondary"
size="sm"
icon="arrow-down-tray"
href="data:text/plain;charset=utf-8,{{ rawurlencode(implode(PHP_EOL, $recoveryCodes).PHP_EOL) }}"
download="pebblework-recovery-codes.txt"
>Download as text</x-ds::button>
</div>
<form method="POST" action="{{ route('two-factor.finish') }}" x-data="{ saved: false }" class="mt-6">
@csrf
<x-ds::checkbox
name="saved"
value="1"
label="I have saved these codes"
x-model="saved"
:error="$errors->first('saved')"
/>
<div class="mt-8 flex justify-end border-t border-divider pt-5">
<x-ds::button type="submit" x-bind:disabled="! saved">Finish</x-ds::button>
</div>
</form>
@endif
</x-ds::panel>
</main>
</x-layouts.base>
Wire it up
This uses Laravel Fortify's two-factor
feature with confirmation turned on, in config/fortify.php:
Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => true,
]),
and the Laravel\Fortify\TwoFactorAuthenticatable trait on User. The view
posts the code straight to Fortify; the rest is two routes of your own:
// routes/web.php
use App\Http\Controllers\Settings\TwoFactorSetupController;
Route::middleware(['auth', 'password.confirm'])->group(function () {
Route::get('/settings/two-factor/{step?}', [TwoFactorSetupController::class, 'show'])
->whereIn('step', ['scan', 'confirm', 'codes'])
->name('two-factor.setup');
Route::post('/settings/two-factor/finish', [TwoFactorSetupController::class, 'finish'])
->name('two-factor.finish');
});
// app/Http/Controllers/Settings/TwoFactorSetupController.php
namespace App\Http\Controllers\Settings;
use Illuminate\Http\Request;
use Laravel\Fortify\Actions\EnableTwoFactorAuthentication;
class TwoFactorSetupController
{
public function show(Request $request, EnableTwoFactorAuthentication $enable, string $step = 'scan')
{
$user = $request->user();
// A secret is made on the first visit and kept until it is confirmed.
if (! $user->two_factor_secret) {
$enable($user);
$user->refresh();
}
// Once confirmed, the only step left is the recovery codes.
if ($user->two_factor_confirmed_at) {
$step = 'codes';
}
return view('settings.two-factor', match ($step) {
'scan' => [
'step' => 'scan',
'qrSvg' => $user->twoFactorQrCodeSvg(),
'setupKey' => decrypt($user->two_factor_secret),
],
'confirm' => ['step' => 'confirm'],
'codes' => ['step' => 'codes', 'recoveryCodes' => $user->recoveryCodes()],
});
}
public function finish(Request $request)
{
$request->validate(
['saved' => ['accepted']],
['saved.accepted' => 'Tick the box once your codes are somewhere safe.'],
);
return to_route('settings.security')->with('status', 'Two-factor authentication is on.');
}
}
Fortify's endpoints, for reference. These paths are Fortify's, under its default prefix:
| Method and path | What it does |
|---|---|
POST /user/two-factor-authentication | Turns it on: makes the secret and recovery codes (the controller above calls the same action) |
POST /user/confirmed-two-factor-authentication | Confirms it with code; named two-factor.confirm, which the view posts to |
DELETE /user/two-factor-authentication | Turns it off |
GET /user/two-factor-qr-code | The QR code as JSON (svg, url), for a page drawn in the browser |
GET /user/two-factor-secret-key | The setup key as JSON |
GET /user/two-factor-recovery-codes | The recovery codes as JSON |
POST /user/two-factor-recovery-codes | New recovery codes |
GET and POST /two-factor-challenge | The code asked for at sign-in |
Only two-factor.confirm is used by name here. Not checked against every
Fortify version: the other routes' names, and the error bag a wrong code
comes back in (confirmTwoFactorAuthentication, which the view reads; if
yours differs, read $errors->first('code')). php artisan route:list --path=two-factor shows what your version registers.
With Livewire, the step is a property, $step, and Continue, Back and
Verify call methods that change it; the OTP input takes
wire:model="code" and @complete="$wire.confirm()", and confirm() calls
Fortify's Laravel\Fortify\Actions\ConfirmTwoFactorAuthentication action
with the user and the code. Bind the checkbox with wire:model.live="saved"
and give Finish :disabled="! $saved"; the x-data and x-bind:disabled
are then not needed.
Phone, keyboard, no script
- On a phone the QR code sits above the setup key, the stepper folds to its markers with "Step 2 of 3" under them, and the recovery codes run in one column.
- Keyboard: on step 2 focus starts in the first box; Tab goes through the boxes' group, Back, Verify. On step 3, Copy codes, Download as text, the checkbox, Finish.
- No script: every step is a link or a form and works. The copy buttons
are drawn but do nothing (the key and the codes are on screen to select),
and the code boxes do not post typed digits (see OTP input);
Finish is not held back, and the server's
acceptedrule refuses an unticked box with the message under it. With the script, Finish stays disabled until the box is ticked.
From the package repo: edit this page on GitHub.