Documentation Index
Fetch the complete documentation index at: https://docs.dynamic.xyz/docs/llms.txt
Use this file to discover all available pages before exploring further.
Device registration protects your users from account takeovers by requiring verification when they sign in from an unrecognized device. For a general overview of the feature, see Device Registration.
Prerequisites
- You need to have the Dynamic Client initialized.
- You need to have device registration enabled in your environment’s settings in the Dynamic Dashboard.
- JavaScript SDK v2 or later.
Checking if device registration is required
After a user authenticates, check whether their current device needs to be registered. This is determined by the device:register scope in the user’s session.
import { isDeviceRegistrationRequired } from '@dynamic-labs-sdk/client';
const user = dynamicClient.user;
if (user && isDeviceRegistrationRequired(user)) {
// Show a banner or prompt to the user
// The user will receive an email with a verification link
}
Completing device registration
When the user clicks the verification link in their email, they are redirected back to your app with a token in the URL. You need to detect this redirect and complete the registration.
import {
detectDeviceRegistrationRedirect,
getDeviceRegistrationTokenFromUrl,
completeDeviceRegistration,
} from '@dynamic-labs-sdk/client';
const url = window.location.href;
if (detectDeviceRegistrationRedirect({ url })) {
const deviceToken = getDeviceRegistrationTokenFromUrl({ url });
const response = await completeDeviceRegistration({ deviceToken });
// Device is now trusted — the user has full access
console.log('Device registered for user:', response.user);
}
Listening for events
You can listen for device registration events to update your UI accordingly.
import { onEvent } from '@dynamic-labs-sdk/client';
// Fires when registration completes in the current tab
onEvent({
event: 'deviceRegistrationCompleted',
listener: () => {
// Hide the registration banner, update UI, etc.
},
});
// Fires when registration completes in another tab
onEvent({
event: 'deviceRegistrationCompletedInAnotherTab',
listener: () => {
// Keep other open tabs in sync
},
});
Getting registered devices
Retrieve all trusted devices for the current user.
import { getRegisteredDevices } from '@dynamic-labs-sdk/client';
const devices = await getRegisteredDevices();
devices.forEach((device) => {
console.log(device.id, device.userAgent, device.isCurrentDevice);
});
Each device includes:
| Field | Type | Description |
|---|
id | string | The device registration ID |
createdAt | string | ISO date of when the device was registered |
userAgent | string | The browser or device user agent |
isCurrentDevice | boolean | Whether this is the device making the request |
Displaying devices
You can use parseUserAgent to display a friendly device name from the raw user agent string.
import { getRegisteredDevices, parseUserAgent } from '@dynamic-labs-sdk/client';
import { useEffect, useState } from 'react';
function RegisteredDevices() {
const [devices, setDevices] = useState([]);
useEffect(() => {
getRegisteredDevices().then(setDevices);
}, []);
return (
<ul>
{devices.map((device) => {
const parsed = parseUserAgent({ userAgent: device.userAgent });
return (
<li key={device.id}>
{parsed?.type === 'mobile' ? '📱' : '💻'}{' '}
{parsed?.displayText ?? 'Unknown device'}
{device.isCurrentDevice && ' (this device)'}
</li>
);
})}
</ul>
);
}
Revoking a device
Remove a single trusted device. If the revoked device is the current device, the user will be logged out.
import { revokeRegisteredDevice } from '@dynamic-labs-sdk/client';
await revokeRegisteredDevice({ deviceRegistrationId: 'device-id' });
Revoking all devices
Remove all trusted devices for the current user. This always logs the user out.
import { revokeAllRegisteredDevices } from '@dynamic-labs-sdk/client';
await revokeAllRegisteredDevices();