---
title: "Alert people to new sign-ins"
description: "Tell a person when their account signs in from a device it has not used before, in the app and by email, with a link to sign that device out from /security."
canonical_url: "https://nuxt-backend.dev/docs/build/auth/accounts/alerts"
---
# Alert people to new sign-ins

> Tell a person when their account signs in from a device it has not used before, in the app and by email, with a link to sign that device out from /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.

- [Source](https://github.com/qruto/nuxt-backend/blob/main/src/convex/client/devices.ts)

A sign-in from a new device is the moment a stolen code or a lost laptop shows. When an account signs in from a device it has not used before, the person hears of it at once. One tap takes them to sign that device out. There is nothing to set up: the alert is on for every app.

No provider sends this email for you. Better Auth tells the package each time a session opens, and the package keeps the devices, sends the alert and records it.

## What the person gets

- **An email** with the device, the time and a link to [`/security`](https://nuxt-backend.dev/docs/build/auth/accounts/security), where the session can be signed out. It goes out through your `setupEmail` instance, in the account's language ([Templates](https://nuxt-backend.dev/docs/build/email/templates)).
- **A notification in the app**, in the [notification inbox](https://nuxt-backend.dev/docs/build/email/notifications).
- **An entry in their recent activity**, on `/security`.

On `/security`, an **Alerts** section (`SecurityAlerts`) lists the latest new-device sign-ins beside the device list, so revoking the session is one click from the alert while it is still signed in. It reads `listSignInAlerts`, which the scaffold's `backend/auth.ts` re-exports from `setupAuth`. The person's [notification preferences](https://nuxt-backend.dev/docs/build/email/preferences#notification-channels) decide which channels they hear on, and the email can be turned off there too.

## Show the alerts in your own markup

`useSignInAlerts()` is the Alerts section without its markup: each alert with its device and time, as one sentence, and a sign-out for its session while that session is still signed in and is not this browser's. Put it on your own security page, or beside your account menu:

```vue [app/components/SignInAlerts.vue]
<script setup lang="ts">
const signIns = useSignInAlerts()
const { t } = signIns.labels
</script>

<template>
  <ul v-bind="signIns.bind.root.value">
    <li v-for="alert in signIns.alerts.value" :key="`${alert.at}:${alert.sessionId ?? ''}`" v-bind="signIns.bind.alert(alert)">
      {{ signIns.sentence(alert) }}
      <button v-if="signIns.canSignOut(alert)" v-bind="signIns.bind.signOut(alert)">{{ t('alertSignOut') }}</button>
    </li>
  </ul>
  <p v-bind="signIns.bind.status.value">{{ signIns.status.value }}</p>
  <p v-if="signIns.error.value" v-bind="signIns.bind.error.value">{{ signIns.labels.errorMessage(signIns.error.value) }}</p>
</template>
```

`bind.root`, `bind.status` and `bind.error` are computed refs, spread with `.value`; `bind.alert(alert)` and `bind.signOut(alert)` return plain objects. The sign-out button keeps focus while it signs out (`aria-disabled`) and is named after the device and the time, so a list of them reads apart. Its words are the `security` labels, `alertNewDevice` and `alertRecovery` among them.

## What counts as a new device

The package keeps a record of the devices each account signed in from: a hash of the device's fingerprint (its browser, its system and its network range), keyed by your auth secret and bound to the account, never the fingerprint itself. A device whose session merely expired is not new, so signing in again on your own laptop next month sends nothing.

- **The first sign-in sends nothing.** A new account has no devices yet, and its first one is not news.
- **An administrator's visit sends nothing.** It opens no session of the person's own.
- **Kept and pruned.** The record is part of the account's [erasure](https://nuxt-backend.dev/docs/build/auth/accounts/data#what-an-erasure-removes), and the hourly maintenance prunes devices not seen for a year.

## How it works

The check runs in Better Auth's `session.create.after` hook, so every new session, from a code or a passkey, compares its device with the record. A new one records `auth.new_device` on the [activity trail](https://nuxt-backend.dev/docs/build/auth/admin/audit-log), and the `notifications` capability turns the event into the in-app notice and the email.

## After a recovery

A passkey-only account that signs in with its recovery code always hears of it, even on a device it knows: the `passkeyRecovered` email says the recovery code was used, from which device, place and time, and that a new passkey must be saved. If the person didn't do it, its link leads to `/security`, which signs every other device out ([Passkey-only accounts](https://nuxt-backend.dev/docs/build/auth/sign-in#passkey-only-accounts)). With the new-device alert, that recovery also counts as a new device.

## React to it in your code

The alert is an event on the trail, so your own code can act on it too, such as holding the account's next export for review. Handle `auth.new_device` in your `onEvent` ([Hear of every event](https://nuxt-backend.dev/docs/build/auth/admin/audit-log#hear-of-every-event)):

```ts [backend/audit.ts]
import { setupAudit, vBackendEvent } from 'nuxt-backend/audit'
import { components, internal } from './_generated/api'
import { internalAction } from './_generated/server'

export const audit = setupAudit(components, { onEvent: internal.audit.onEvent })

export const onEvent = internalAction({
  args: { event: vBackendEvent },
  handler: async (ctx, { event }) => {
    if (event.type === 'auth.new_device' && event.subjectId) {
      await ctx.runMutation(internal.reviews.flag, { userId: event.subjectId })
    }
  },
})
```

## What happens when…

- **Email isn't connected.** The alert still lands in the app and on the trail. The email waits for `EMAIL_API_KEY`, and in development prints nothing.
- **The address is suppressed.** The email is skipped, as every send to a [suppressed address](https://nuxt-backend.dev/docs/build/email/delivery/suppressions) is, and the in-app notice still shows.
- **The person turned the email off.** Their [preferences](https://nuxt-backend.dev/docs/build/email/preferences#notification-channels) win, except after a recovery, which always sends.

## Limits

- **A fingerprint is a guess.** A browser update or a new IP range can make a known device look new, and a careful attacker can copy a fingerprint. Treat the alert as a prompt to look, not a verdict.
- **One email per device per day.** A device that keeps losing its record alerts at most once a day.
- **Every alert is an email** against your provider's quota ([Resend](https://nuxt-backend.dev/docs/services/resend)).

## Test it

```ts [backend/alerts.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('the first sign-in sends no alert', async () => {
  const ada = await asUser(testApp())
  const activity = await ada.query(api.audit.listActivity, { paginationOpts: { numItems: 20, cursor: null } })
  expect(activity?.page.map(entry => entry.type)).not.toContain('auth.new_device')
})
```

## Related

- [Manage passkeys and sessions](https://nuxt-backend.dev/docs/build/auth/accounts/security): where the alert leads.
- [Show in-app notifications](https://nuxt-backend.dev/docs/build/email/notifications): the inbox the alert lands in.
- [Record and review activity](https://nuxt-backend.dev/docs/build/auth/admin/audit-log): the trail the alert is written to.
- [Require a recent sign-in](https://nuxt-backend.dev/docs/build/auth/access/step-up): the other guard against a session that is not the person's.


## Sitemap

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