Skip to content
Bladewell

<x-widget.otp>

OTP Blade component for Laravel and Livewire

One-time passwords in one box per character, digits or letters, with a pasted code filling them all. Filled, outline or underline boxes, groups with a dash between, and masked for a PIN. Submits one value. Works with Livewire wire:model. For a code sent by text or email, prefer the verification code: one field works better with the phone's code autofill, pasting and screen readers than a box per character.

php artisan bladewell:add otp
Works with
Laravel 12 and 13, Livewire 3 and 4, Tailwind CSS 4.1+, PHP 8.3+
Guide
Two-factor authentication screens for Laravel Fortify

Usage

Livewire

In a Livewire component, bind with wire:model; no name is needed. The boxes fill one hidden input, and that's what's bound, so the property holds the whole code (123456). When the last box is filled the component fires otp-complete, which Alpine (bundled with Livewire) can hand to a method: no Verify button needed. Set the property to '' in PHP, after a wrong code, and the boxes empty.

Blade
<div x-on:otp-complete="$wire.verify()">
    <x-widget.otp label="Code we sent you" :length="6" wire:model="code" />
</div>

On the server

The OTP can't stop a request that skips it. Here's what to check on your server, and what to keep cheap.

Security

  • Store the code hashed, as you would a password, with an expiry (10 minutes is common). To use it, check it with Hash::check() (never ==), then mark it used in one conditional update that only succeeds while it's unused: Code::whereKey($code->id)->whereNull('used_at')->update(['used_at' => now()]) === 1. Two submissions at once can both pass the check; only one wins the update, so the code works once.

    OWASP: Multi-factor authentication · Laravel: Hashing

  • Consider allowing a few tries per code (5 is common) and then asking for a new one, and throttling by account as well as IP: a 6-digit code is only a million guesses.

    OWASP: Authentication · Laravel: Rate limiting

Server cost

  • Each resend is an email or SMS, and SMS is billed per message. Consider one resend a minute and a daily cap per account and per phone number, which also stops people using your form to send texts to numbers they don't own.

    Laravel: Rate limiting

Examples

Otp

One box per digit. Typing moves to the next box, Backspace to the previous, and a pasted code fills them all. Once every box is filled it fires otp:complete, for a form that submits itself.

Show code
Blade
<x-widget.otp name="otp" label="One-time password" />

Box styles

variant is filled (default), outline or underline. group="3" adds a dash between groups of boxes. masked shows dots, for a PIN. alphanumeric takes letters too, shown in capitals. length sets how many boxes.

Show code
Blade
<div class="grid gap-8 sm:grid-cols-2">
    <x-widget.otp name="otp_outline" label="Outline, grouped" variant="outline" group="3" />
    <x-widget.otp name="otp_underline" label="Underline" variant="underline" placeholder="" />
    <x-widget.otp name="pin" label="PIN" :length="4" masked placeholder="" class="max-w-60" />
    <x-widget.otp name="invite" label="Invite code" alphanumeric variant="outline" placeholder="" />
</div>

Props

Other attributes, such as autocomplete or data-*, are passed through to the element. class styles the component's outer wrapper.

<x-widget.otp>

Prop Default Description
name null What it submits as; its error and old input are found under it. Optional with wire:model, which then names it.
id null Defaults to one made from the name (or the wire:model property).
length 6 How many boxes.
label null Shown above the field, and its name for screen readers.
value null The value to show. Old input wins after a failed submit; with wire:model and no value, the bound Livewire property.
placeholder '0' Shown in each empty box.
error null An error message of your own; otherwise the validation error for the name, from the session or Livewire.
info null A hint under the boxes.
bag 'default' Which error bag to read the error from.
disabled false Greyed out: it can't be changed.
readonly false Shown, and submitted, but it can't be changed. With disabled, disabled wins.
autofocus false Focuses the first box when the page loads.
variant 'filled' filled, outline or underline.
group null Splits the boxes into groups of this size (3: 123 456).
masked false Hides each character as it's typed, like a password.
alphanumeric false Letters and digits (shown in capitals) instead of digits only.
required-message 'Enter the whole code.' Shown when the form is submitted with required and the code isn't all there.

Accessibility

All 2 examples above are checked with axe-core against the WCAG 2.2 A and AA rules, in the light theme and the dark one, both as the page draws and with each popover, dialog, toast and tooltip opened, on every change. A change that fails can't be merged. Where axe can't decide, such as contrast on SVG text, the test measures the colours itself instead of letting it pass.

Automated checks can't judge everything: how it sounds in a screen reader, and how it feels to use from the keyboard, still need a person. Check those on your own pages too.

Source

What bladewell:add otp writes to your app with the default namespaces. Prefer to copy by hand? Take these files, plus the ones from Field, and the theme and base CSS.

resources/views/components/widget/otp/index.blade.php Show
index.blade.php
resources/js/widget/otp/index.js Show
index.js
app/View/Widget/ElementIds.php Show
ElementIds.php
app/View/Widget/FormField.php Show
FormField.php