Skip to main content

Device code

The code that a person types on another device, with the address to open and the state of the wait.

PatternReadyView source
Works with
Reactshadcn/ui
Standards
RFC 8628
Runs on sample data, with the neutral theme of shadcn/ui.

The screen of a TV app, of a CLI in a browser tab, or of an agent that enrolls itself. It shows a short code, says where to type it, and waits. The preview "approves" by itself after a few seconds.

Installation​

The command adds the file to your project, with the shadcn/ui components that it uses. Your project needs shadcn/ui.

pnpm dlx shadcn@latest add https://examples.lumoauth.dev/r/device-code.json

Usage​

fromDeviceAuthorization maps the answer of the device authorization endpoint. Your code polls the token endpoint and sets the status.

app/tv/sign-in.tsx
'use client';

import * as React from 'react';
import { DeviceCode, fromDeviceAuthorization, type DeviceCodeData, type DeviceCodeStatus } from '@/components/device-code';

export function TvSignIn() {
const [code, setCode] = React.useState<DeviceCodeData | null>(null);
const [status, setStatus] = React.useState<DeviceCodeStatus>('waiting');

const start = React.useCallback(async () => {
const response = await fetch('/api/device/start', { method: 'POST' }).then((r) => r.json());
setCode(fromDeviceAuthorization(response));
setStatus('waiting');
}, []);
React.useEffect(() => void start(), [start]);

// Poll your backend, which polls the token endpoint with the device code.
React.useEffect(() => {
if (!code || (status !== 'waiting' && status !== 'slow_down')) return;
const timer = setInterval(async () => {
const { state } = await fetch('/api/device/poll').then((r) => r.json());
if (state !== 'authorization_pending') setStatus(state === 'access_denied' ? 'denied' : state === 'expired_token' ? 'expired' : state);
}, 5000);
return () => clearInterval(timer);
}, [code, status]);

return code ? <DeviceCode code={code} status={status} deviceName="this TV" onRestart={start} /> : null;
}

The pattern has no QR library of its own. Pass a QR code of verificationUriComplete as qr, drawn with the library that your project uses. The page that the person opens is the hosted device activation page.

Where the data comes from​

What the pattern showsCall
The code and the addressPOST /oauth/device_authorization: user_code, verification_uri, verification_uri_complete, expires_in, interval
The waitPOST /oauth/token with the device grant type: authorization_pending, slow_down, access_denied, expired_token
An agent that enrolls itselfPOST /agents/enrollments, then POST /agents/enrollments/token

States​

Runs on sample data, with the neutral theme of shadcn/ui.
  • Slow down. The server asked for a longer interval.
  • Connected.
  • Denied, with "Get a new code".
  • Expired. A waiting code whose time ran out shows this state by itself.

Props​

PropTypeDefaultDescription
coderequiredDeviceCodeDataThe code: userCode, verificationUri, verificationUriComplete and expiresAt.
statusrequired'waiting' | 'slow_down' | 'connected' | 'denied' | 'expired'The state of the wait.
deviceNamestring'this device'What connects, for example "this TV".
qrReactNodeA QR code of verificationUriComplete.
onRestart() => voidAsk for a new code. The button appears when the code expired or was denied.
classNamestringClass name for the card.