Skip to main content
Urbicon UI
source

TwoFactorManagerbeta

Self-service two-factor (TOTP) management: enrol with an authenticator app, show one-time backup codes, and disable with a password re-auth. Pass a `qr` snippet to render the setup QR code; the package ships no QR encoder, so without one the otpauth URI and Base32 secret are shown as text to type in.

01 Usage

Basic

The live preview runs against a mocked demo API (injected via the fetcher prop): setup returns a demo secret and URI, and any 6-digit code reveals demo backup codes (nothing is verified). No qr snippet is passed here, so the page falls back to manual entry. The code below shows the production setup.

Two-factor authentication

Add a second step to sign-in using an authenticator app.

<script lang="ts">
  import { TwoFactorManager } from '@urbicon-ui/auth';
  import type { AuthUser } from '@urbicon-ui/auth';

  // `user` typically comes from your auth store or `locals.user`.
  let { user }: { user: AuthUser | null } = $props();
</script>

<TwoFactorManager {user} apiPath="/api/auth/account/2fa" />

02 Accessibility

The error region outlives the step it came from

The role="alert" region sits directly under the heading, above the idle/setup/backup branch rather than inside it. An error raised while confirming a code is therefore still announced after the view changes; a region nested in the branch would unmount with the step before the reader heard it.

The secret and the backup codes are text elements

The TOTP secret renders in a <code> element and the backup codes in a <ul> of <li>. A screen reader announces the list with its item count and steps through the codes one at a time, where the same content in a grid of styled divs would be an unnavigable run of characters.

Autofill hints on both entry paths

Disabling 2FA re-authenticates with an autoComplete="current-password" field; the setup code uses inputmode="numeric" with autoComplete="one-time-code", which brings up the numeric keypad and lets the OS offer the code directly.

Advancing a step moves focus to its heading

Moving from idle to setup to backup codes swaps the content in place, so the button that was pressed is gone. Each step opens with an <h3> carrying tabindex="-1", and focus lands there once the new step has rendered — the reader hears "Save your backup codes" instead of falling to <body>. Turning 2FA off does the same: the disable form is replaced by the enable button, and focus goes to the panel heading, which is also where a cancelled setup returns. A refused code changes no step, so nothing moves and the caret stays in the field being corrected. The QR code is your snippet, so its alternative text is yours to provide; the secret sits next to it as text for anyone who cannot scan.

03 API Reference

12 props 1 required
Prop
Type
Default
Description

04 Types

Local type definitions used by this component.

6 types
Name
Kind
Category
Used by
Description

05 Installation

Import

import { TwoFactorManager } from '@urbicon-ui/auth';