---
title: "Manage passkeys and sessions"
description: "Let a signed-in person add and remove passkeys, turn sign-in codes off for a passkey-only account, see and sign out their devices, read their account's recent activity, and reach its export and deletion, all on /security."
canonical_url: "https://nuxt-backend.dev/docs/build/auth/accounts/security"
---
# Manage passkeys and sessions

> Let a signed-in person add and remove passkeys, turn sign-in codes off for a passkey-only account, see and sign out their devices, read their account's recent activity, and reach its export and deletion, all on /security.

Version: nuxt-backend 0.6.x · Tab: Build · Kind: how-to · Status: stable

Every page: [llms.txt](https://nuxt-backend.dev/llms.txt) · Skill for coding agents: [nuxt-backend](https://nuxt-backend.dev/.well-known/skills/nuxt-backend/SKILL.md)

Commands use npm; use your project's package manager.

- [Try it live in the playground](https://nuxt-backend.dev/playground/security)
- [API reference](https://nuxt-backend.dev/docs/reference/api/use-sessions)
- [Source](https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/components/account/SecuritySettings.vue)

A person who fears for their account needs one place to act: see every device signed in, sign the strange one out, and add a passkey. The module mounts `/security` (module option `pages.security`), guarded for signed-in people. It renders `<SecuritySettings>` in sections you pick with `sections`: `passkeys`, `twoFactor` (an authenticator app), `sessions`, `alerts` and `danger` (signing this browser out). Below it, `<AccountData>` exports and deletes the account ([Export and delete an account](https://nuxt-backend.dev/docs/build/auth/accounts/data)).

Better Auth keeps the passkeys and the sessions. The package adds the page, a session list that works at any age, the recent activity, and the "Confirm it's you" step before a passkey changes. It also keeps the browser's passkey list in step, and lets a person turn sign-in codes off.

```vue [app/pages/security.vue]
<script setup lang="ts">
definePageMeta({ auth: true })
</script>

<template>
  <SecuritySettings :sections="['passkeys', 'sessions']" />
</template>
```

## Passkeys

`usePasskeys()` lists the person's passkeys and adds, renames and removes them:

```vue [app/components/PasskeyList.vue]
<script setup lang="ts">
const { passkeys, add, remove, isPending, error } = usePasskeys()
const { withReauthentication } = useReauthentication()
</script>

<template>
  <ul>
    <li v-for="passkey in passkeys" :key="passkey.id">
      {{ passkey.name }}
      <button type="button" @click="withReauthentication(() => remove(passkey.id))">Remove {{ passkey.name }}</button>
    </li>
  </ul>
  <button type="button" :aria-disabled="isPending || undefined" @click="withReauthentication(() => add({ name: 'Laptop' }))">Add a passkey</button>
  <p v-if="error" role="alert">{{ error.message }}</p>
</template>
```

- **Adding and removing take a recent sign-in.** `<SecuritySettings>` asks with the dialog it renders. Your page wraps the call in `withReauthentication`, as above. Renaming never asks.
- **Failures.** The list loads in the browser, and a failed load lands in `error`. `add`, `rename` and `remove` reload the list, and reject on failure, a cancelled passkey prompt included.
- **An administrator's visit** sees the passkeys and can change none of them: a passkey would let them back in after the visit.
- **The device follows.** Loading the list, which a removal reloads, tells the browser which passkeys the account still holds (`signalAllAcceptedCredentials`), so a removed one stops showing in the device's prompt. A name or address change updates the passkey's label (`signalCurrentUserDetails`) ([Keep the browser's passkey list in step](https://nuxt-backend.dev/docs/build/auth/sign-in#keep-the-browsers-passkey-list-in-step)).

### What this browser supports

`usePasskeys().capabilities` holds what the browser's `getClientCapabilities()` reported, or `null` before it answers or in a browser without it. Read it to hide a button that can't work:

```vue [app/components/AddPasskey.vue]
<script setup lang="ts">
const { add, capabilities } = usePasskeys()
const { withReauthentication } = useReauthentication()
</script>

<template>
  <button
    v-if="capabilities?.passkeyPlatformAuthenticator !== false"
    type="button"
    @click="withReauthentication(() => add({ name: 'This device' }))"
  >
    Add a passkey
  </button>
</template>
```

The keys are the browser's own, such as `conditionalGet`, `hybridTransport` and `passkeyPlatformAuthenticator` ([WebAuthn client capabilities](https://web.dev/articles/webauthn-client-capabilities)). `useLoginFlow` reads the same answer before it offers a passkey.

## Passkey-only

A person with a passkey can turn sign-in codes off, so a stolen mailbox can't sign in to the account. `<SecuritySettings>` shows the switch in its `passkeys` section, labeled "Sign in with passkeys only", once the account has a passkey; while it is on, "Make a new recovery code" issues a new one.

```vue [app/components/PasskeyOnly.vue]
<script setup lang="ts">
const { passkeyOnly, setPasskeyOnly, passkeys } = usePasskeys()
const { withReauthentication } = useReauthentication()
const recoveryCode = ref<string | null>(null)

async function turnOn() {
  const answer = await withReauthentication(() => setPasskeyOnly(true))
  recoveryCode.value = answer.recoveryCode
}
</script>

<template>
  <button v-if="!passkeyOnly" type="button" :aria-disabled="!passkeys?.length || undefined" @click="turnOn">
    Sign in with passkeys only
  </button>
  <button v-else type="button" @click="withReauthentication(() => setPasskeyOnly(false))">
    Allow sign-in codes again
  </button>
  <p v-if="recoveryCode">
    Your recovery code: <code>{{ recoveryCode }}</code>. Keep it somewhere safe. It is shown once.
  </p>
</template>
```

- **passkeyOnly** is `true` while the account refuses sign-in codes.
- **setPasskeyOnly(on)** takes a recent sign-in, and refuses an administrator's visit. Turning it on needs at least one passkey, and resolves with `{ recoveryCode }`. Turning it on again issues a new code, and the old one stops working.
- **Removing the last passkey** of a passkey-only account is refused, so the account can't lock itself out. Allow codes again first.
- **Recovery.** "Lost your passkey?" on the sign-in page takes the address and the recovery code, then asks for a new passkey before anything else ([Passkey-only accounts](https://nuxt-backend.dev/docs/build/auth/sign-in#passkey-only-accounts)). The person gets an email saying so, and the sign-in page shows the new recovery code that replaced the used one, once. "Make a new recovery code" here replaces it again.

Store the recovery code as a person would store a password: in a password manager, or on paper. The package keeps only its SHA-256 hash, tagged for recovery and bound to the account, so it can't be used as a sign-in code or an email-change code. The hash takes no key, so rotating the auth secret never stops a code working. The account's session and `getAuthUser` never return it.

## An authenticator app

An account can ask for a second step after its sign-in code: a six-digit code from an authenticator app (TOTP). A passkey sign-in asks for none, since the passkey is a second factor of its own. `useTwoFactor()` holds it, and the `twoFactor` section renders it:

- **start()** makes a new secret and ten backup codes, in `pendingSetup`: the `otpauth://` link an app opens (`totpURI`), its key for an app that takes it typed (`secret`), and the `backupCodes`, shown once. Nothing asks for the app yet.
- **confirm(code)** checks the app's first code and turns the step on (`enabled`). A wrong code is `invalid_input` on `twoFactorCode`, and the setup waits.
- **disable()** turns it off, and **regenerateBackupCodes()** replaces the backup codes, the old ones then stop working.
- `start`, `disable` and `regenerateBackupCodes` take a recent sign-in, and refuse an administrator's visit. The section wraps them in "Confirm it's you".

At sign-in, a code that checks for such an account opens no session: `<AuthForm>` moves to its `two-factor` step, which takes the app's code or a backup code, and "Don't ask again on this device for 30 days". The step lasts ten minutes; after that, or after too many wrong codes, a new sign-in code starts it again. The secret and the backup codes are stored encrypted under `AUTH_SECRET`, and the account's export says only whether the app is on.

## Signed-in devices

`useSessions()` is a live list of the person's unexpired sessions, the latest expiry first, with `current` marking this browser. Each session carries `device`, a readable label such as `Chrome · macOS`, or `null` when the browser said nothing useful.

- `revoke(id)` signs one session out, and `revokeOthers()` every other one. Each rejects with what the call threw.
- While the session itself is still being read, server rendering included, `sessions` is `undefined` and `isLoading` is `true`, never an empty list.
- A failed read lands in `listError`, apart from an action's failure in `error`.
- It works for sessions of any age. Better Auth's own session list refuses a sign-in older than ten minutes, so the package reads the session table instead, and returns ids, never session tokens.

A sign-in from a new device also tells the person, in the app and by email, with a link back here ([Alert people to new sign-ins](https://nuxt-backend.dev/docs/build/auth/accounts/alerts)).

## Recent activity

Below the sections, the page lists what happened to the account, newest first, ten at a time. It shows sign-ins and sign-outs, passkeys, email changes, devices signed out, workspaces joined or left, and what an administrator did to the account. It is `<AuditLog>` over the [activity trail](https://nuxt-backend.dev/docs/build/auth/admin/audit-log), which keeps a year of it.

## Deletion and sign-out

`<AccountData>`, below the sections, deletes the account. The person types their address and confirms it's them, and the account is deleted after a grace period ([Delete an account](https://nuxt-backend.dev/docs/build/auth/accounts/data#delete-an-account)). Removing a passkey asks first: in the component's own styled dialog, or through the `confirm` prop when you pass one.

Signing out here goes to the sign-in page. A sign-out in another tab, or this session revoked from another device, ends the session under the open page. The module then sends the person to sign in, with `?redirect=` back to `/security` ([When the session ends](https://nuxt-backend.dev/docs/build/auth/access/pages#when-the-session-ends)).

## Its props, events and slots

`<SecuritySettings>` takes `sections`, `confirm` and `labels`. It emits `passkey-added`, `passkey-removed(id)`, `two-factor-enabled`, `two-factor-disabled`, `session-revoked(id)`, `sessions-revoked`, `signed-out` and `error(message)`. Its slots are `passkeys`, `passkey`, `twoFactor`, `sessions`, `session`, `alerts`, `danger` and `footer`. Its words are the `security` label group ([Translate the UI](https://nuxt-backend.dev/docs/build/ui/customize/translation)). Each row's buttons are named after the passkey or the session they act on.

To restyle it, the customization ladder applies: tokens, labels, slots, then swapping a part such as `SecurityPasskeys` or `SecurityTwoFactor` with a component of yours of the same name ([Customization ladder](https://nuxt-backend.dev/docs/build/ui/customize)).

## What happens when…

<table>
<thead>
  <tr>
    <th>
      Refused with
    </th>
    
    <th>
      When
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a href="https://nuxt-backend.dev/docs/reference/backend/errors#reauthentication_required">
        <code>
          reauthentication_required
        </code>
      </a>
    </td>
    
    <td>
      Adding or removing a passkey, or switching passkey-only, with a sign-in older than ten minutes. The dialog asks, then the action runs again.
    </td>
  </tr>
  
  <tr>
    <td>
      <a href="https://nuxt-backend.dev/docs/reference/backend/errors#forbidden">
        <code>
          forbidden
        </code>
      </a>
      
      , reason <code>
        impersonating
      </code>
    </td>
    
    <td>
      An administrator's visit tries to change a passkey or passkey-only.
    </td>
  </tr>
  
  <tr>
    <td>
      <a href="https://nuxt-backend.dev/docs/reference/backend/errors#invalid_input">
        <code>
          invalid_input
        </code>
      </a>
      
      , field <code>
        passkeys
      </code>
    </td>
    
    <td>
      Passkey-only is turned on without a passkey, or the last passkey of a passkey-only account is removed.
    </td>
  </tr>
  
  <tr>
    <td>
      <a href="https://nuxt-backend.dev/docs/reference/backend/errors#unavailable">
        <code>
          unavailable
        </code>
      </a>
    </td>
    
    <td>
      The browser cancelled the passkey prompt, or has no passkey provider.
    </td>
  </tr>
</tbody>
</table>

## Limits

- **Signals and capabilities** need Chrome or Edge 132, or Safari 26. Elsewhere the page works the same, and a removed passkey stays on the device until the person deletes it there.
- **One recovery code** per passkey-only account. Without it and without a passkey, only an administrator can turn passkey-only off.
- **Sessions** list the first 100 per account.

## Test it

A function test calls the same routes as the page. `asUser` opens a fresh session, so the recent sign-in passes ([Test backend functions](https://nuxt-backend.dev/docs/develop/testing/functions)):

```ts [backend/security.test.ts]
import { asUser, defineBackendTestApp } from 'nuxt-backend/test'
import { expect, test } from 'vitest'
import { api } from './_generated/api'
import schema from './schema'

const testApp = defineBackendTestApp(schema, import.meta.glob('./**/*.ts'))

test('lists the caller\'s sessions, never their tokens', async () => {
  const ada = await asUser(testApp(), { email: 'ada@example.com' })
  const sessions = await ada.query(api.auth.listSessions, {})
  expect(sessions).toHaveLength(1)
  expect(sessions[0]).not.toHaveProperty('token')
})
```

## Coming from the current API

`usePasskeys()` gained `capabilities`, `passkeyOnly` and `setPasskeyOnly`, and `<SecuritySettings>` the passkey-only switch.

## Related

- [Sign users in](https://nuxt-backend.dev/docs/build/auth/sign-in#passkey-only-accounts): the recovery flow of a passkey-only account.
- [Export and delete an account](https://nuxt-backend.dev/docs/build/auth/accounts/data): `<AccountData>`, below these sections.
- [Require a recent sign-in](https://nuxt-backend.dev/docs/build/auth/access/step-up): the dialog adding or removing a passkey opens.
- [Record and review activity](https://nuxt-backend.dev/docs/build/auth/admin/audit-log): the trail the recent activity reads.
- [Account components](https://nuxt-backend.dev/docs/reference/components/account#securitysettings): `<SecuritySettings>` in full.


## Sitemap

See the full [sitemap](https://nuxt-backend.dev/sitemap.md) for all pages.
