Accounts
Auth
Auth covers sign-in, sessions, second factors, email verification and the
account pages under /dashboard/settings. It is switched from the auth block
in starter.config.ts(Licensees only). You work here when you pick the
sign-in methods, add an OAuth provider or protect a route of your own.
How it works
layers/kit/modules/auth-routes.ts(Licensees only) readsauthat build time and mounts only the routes it asks for, fromlayers/kit/server/auth/routes/(Licensees only). A method you turn off has no endpoint.- Every sign-in goes through
openSessioninlayers/kit/server/utils/auth.ts(Licensees only). It refuses a suspended or deletion-scheduled account, works out whether a second factor is still owed, writes a row insessions, and seals the nuxt-auth-utils cookie with that row's id (sid) and the account'ssession_version. requireSessionreads the row back on every call. A session ends when its row is revoked, when the account's version is bumped, after 30 days without a request, or when its cookie turns 30 days old. Thesessions:prunetask deletes dead rows onschedule.cleanup(daily by default, Background jobs).- Mailed links (verification, magic link, password reset, restore) open a page that posts the token when the person clicks. A mail scanner that prefetches the link spends nothing.
Choose how people sign in
auth: {
enabled: true,
primary: 'password',
oauth: ['github'],
totp: true,
passkey: 'login',
emailVerification: true,
},
primary | Sign-in | New accounts |
|---|---|---|
'password' | Email and password on /auth/login | /auth/register |
'passwordless' | A magic link mailed from /auth/login | The first magic link sent to a new address |
'none' | OAuth only | The first OAuth sign-in with a trusted address |
oauthworks with anyprimary. It takes'github','google'and'microsoft', or the id of a handler you drop intoserver/auth/adapters/(Add a provider the kit doesn't wire).totpadds authenticator codes as a second factor. It is never a way in on its own.passkey: 'login'lets a passkey sign in alone and skips TOTP.'second-factor'only confirms a session the primary method opened.emailVerificationapplies underprimary: 'password'. Under'passwordless'an account whose address nobody has proven always waits for its first sign-in by email link, whatever the flag says (Verify email addresses).
/auth/register exists only under 'password'. On other builds it answers 404 and
every sign-up link points at /auth/login. Password reset needs mail, so with
email.enabled: false its routes are not mounted and the "Forgot password?"
link disappears. The build fails on primary: 'none' when nothing could ever
sign in, and warns when existing users can sign in but nobody new can register,
for example with Microsoft as the only provider.
Add GitHub sign-in
- Set
oauth: ['github']. - On GitHub, create an OAuth app with the callback URL
https://<your-domain>/api/auth/oauth/github. - Put its credentials in
.env, and in the Worker's secrets for a deploy:NUXT_OAUTH_GITHUB_CLIENT_ID=... NUXT_OAUTH_GITHUB_CLIENT_SECRET=... - Restart
pnpm dev. The login page shows a GitHub button.
A GitHub OAuth app takes one callback URL, so register a second app for local
development with http://localhost:3000/api/auth/oauth/github. The button
errors until its client id and secret are set.
/api/auth/oauth/<provider> is both the leg that leaves for the provider and
the callback. A ?redirect= on the first leg is kept in a cookie for ten
minutes and applied after sign-in, so an invitation link survives the round
trip. Google works the same way with NUXT_OAUTH_GOOGLE_*. Microsoft also
needs NUXT_OAUTH_MICROSOFT_TENANT.
Only the social sign-in started last can finish. nuxt-auth-utils keeps one pending sign-in for all providers, so with two sign-in tabs open the older one returns to the login page with an error.
Which providers can create accounts
An OAuth identity never joins an existing account because the email matches. It signs in only once it is attached to an account: either it created the account, or a signed-in user connected it from Settings → Security. Signing in with a provider whose address already has an account is refused, and the login page explains how to connect it instead.
Whether a provider's address may open a new account is its trust rule. The
shipped three's rules are EMAIL_TRUST in
layers/kit/shared/starter/oauth-trust.ts(Licensees only):
| Provider | Rule | Opens accounts |
|---|---|---|
| GitHub | 'provider-confirmed' | Yes |
'verified-flag' | Only when Google sends email_verified: true | |
| Microsoft | false | Never. It can only be connected from Settings |
A provider you drop in states its rule as auth.adapters.<id>.emailTrust, one
of the same three values. oauthEmailTrust(provider) reads that declaration
first and EMAIL_TRUST second. A shipped provider's rule can only be restated
by replacing its handler
(Replace a provider the kit ships).
completeOAuthCallbackopens no account for a provider whose rule isfalse, whatever address its handler passes. The identity can still be connected from Settings, and one already attached still signs in.'verified-flag'is checked inoauthCallbackHandlers, which readsemail_verifiedfrom the provider's payload. AnOAuthIdentitycarries no such flag, so a handler that builds its own identity must passemail: nullunless the provider said the address is verified.- An id with no rule, shipped or declared, is left alone, so a route of your
own outside
auth.oauththat callscompleteOAuthCallbackkeeps working. - A dropped-in provider counts like a shipped one: toward the
primary: 'none'check when its rule is notfalse, and as a way in for thelast-sign-in-methodguard whatever its rule.
Add a provider the kit doesn't wire
nuxt-auth-utils ships many more handlers than the three the kit wires. To add one, say LinkedIn:
- Create
server/auth/adapters/linkedin.ts. The file name is the provider id: one lowercase word, and notpassword,passkeyoremail. Its default export answersGET /api/auth/oauth/linkedin, both the leg that leaves for the provider and the callback:export default withOAuthState( 'linkedin', defineOAuthLinkedInEventHandler({ ...oauthCallbackHandlers('linkedin') }), ) - List it in
auth.oauthand declare it inauth.adapters:auth: { oauth: ['linkedin'], adapters: { linkedin: { label: 'LinkedIn', icon: 'i-fa6-brands-linkedin', emailTrust: 'verified-flag', credentials: ['clientId', 'clientSecret'], }, }, }, - On LinkedIn, register the callback URL
https://<your-domain>/api/auth/oauth/linkedin, then setNUXT_OAUTH_LINKEDIN_CLIENT_IDandNUXT_OAUTH_LINKEDIN_CLIENT_SECRET. - Restart
pnpm dev. The login page shows a LinkedIn button.
label is shown as written, untranslated. icon is an Iconify name from a
collection the app installs. Each key in credentials reads
NUXT_OAUTH_<ID>_<KEY> into useRuntimeConfig(event).oauth.<id>, where the
handler finds it. A declared key left unset reads as an empty string, which
the library's handler uses in place of its own default, so set every key you
declare. Without credentials, the kit adds no keys and one JSON variable
fills the block, such as
NUXT_OAUTH_LINKEDIN='{"clientId":"…","clientSecret":"…"}'. Listing and
declaring the provider is all the login button, the Settings → Security row
with its Connect and Disconnect, the sign-in method count and the login page's
refusal copy need.
withOAuthState, oauthCallbackHandlers and the library's
defineOAuth…EventHandler factories are auto-imported. oauthCallbackHandlers
reads id or sub, email or mail, and name or displayName from the
provider's payload. For a provider the library lacks, or one that names those
differently, write the handler yourself: check your own state or PKCE, build
an OAuthIdentity (the type is in #layers/kit/server/utils/oauth), and end
with
sendRedirect(event, localeRedirect(event, await completeOAuthCallback(event, identity))).
On a failure, redirect to /auth/login?error=oauth&provider=<id> the same way.
- The kit wraps a dropped-in handler in
withSignInRedirectonly, never inwithOAuthState, so thestatecheck is your handler's. A handler that checks none reopens login CSRF. - A handler that writes
oauth_accountsor opens a session itself, instead of callingcompleteOAuthCallback, skips the rule that an identity never joins an account by its address. - Handlers built on the library's factories share its one pending sign-in with the shipped three, so the sign-in started last wins.
Replace a provider the kit ships
A github.ts, google.ts or microsoft.ts of yours in
server/auth/adapters/ replaces the handler the kit generates for that
provider, and the build logs which file won. It is written like a dropped-in one
and wrapped the same way, in withSignInRedirect only, so it calls
withOAuthState itself as step 1 of Add a provider the kit doesn't
wire does. A GitHub replacement passes
config: { emailRequired: true } as the generated handler does, or a GitHub
account with no public address returns none. auth.adapters.<id> may then
restate label, icon or emailTrust. What it leaves out stays as shipped.
Without a replacement, an entry on a shipped provider may declare only
credentials. Each key is added to the options the library's handler reads,
which covers GitHub Enterprise:
adapters: {
github: { credentials: ['authorizationURL', 'tokenURL', 'apiURL'] },
},
These read NUXT_OAUTH_GITHUB_AUTHORIZATION_URL, NUXT_OAUTH_GITHUB_TOKEN_URL
and NUXT_OAUTH_GITHUB_API_URL, and the Worker's [env] warning names any
left unset (Deployment). An unset
key keeps the library's default, github.com's own URL, rather than an empty
string. A replaced handler that reads a declared key from
useRuntimeConfig(event).oauth.<id> gets null while it is unset.
A replaced provider is still asked for its NUXT_OAUTH_<PROVIDER>_CLIENT_ID /
_CLIENT_SECRET pair at boot, and a replaced Microsoft also for
NUXT_OAUTH_MICROSOFT_TENANT, because the boot check can't see the
replacement. A handler that doesn't read them can ignore that warning.
Protect a route
On the server, call a session helper first. They are auto-imported from
layers/kit/server/utils/auth.ts:
export default defineEventHandler(async (event) => {
const session = await requireVerifiedSession(event)
return { id: session.user.id, email: session.user.email }
})
| Helper | Answers |
|---|---|
requireVerifiedSession(event) | 401 when signed out, 403 second-factor-required while a factor is owed |
requireSession(event) | 401 when signed out. Lets a session that still owes a factor through |
verifySession(event) | The session, or null. Never throws |
requirePermission(event, 'users:manage') | Also checks a role or plan permission (Permissions) |
Each one reads the session row, so a revoked, banned or version-bumped session
is refused on its next request. getUserSession from nuxt-auth-utils only
unseals the cookie and checks none of that, so don't use it to decide who
someone is.
In a page, the auth middleware sends a signed-out visitor to
/auth/login?redirect=<path>:
<script setup lang="ts">
definePageMeta({ layout: 'dashboard', middleware: 'auth' })
const { user, session } = useUserSession()
</script>
user is a SessionUser from
layers/kit/shared/types/session.ts(Licensees only):
id, email, name, role, avatar, emailVerified and termsCurrent.
loggedIn is already true for a session that still owes its second factor, so
code that means "signed in" also checks session.value?.pendingMfa, as the
middleware does.
Verify email addresses
An account whose address nobody has proven yet waits for proof:
- With
primary: 'password'andemailVerification: true, signup mails a confirmation link that lasts 24 hours. The link counts only when it is opened in a browser signed in to that account; signed out,/auth/verify-emailasks the person to sign in and comes back. - Under
passwordwith email on, an account that has never proven an address and holds no way to sign in, such as the one guest checkout creates, is proven only by a password reset. The toast's resend and a verify link answer 409set-password-first, and an address-change link answers 409set-password-before-change, both without spending the link. The customers page offers staff no verification email for it, and that route answers 409no-password-yet./auth/verify-emailsends the person to Forgot password. - With
primary: 'passwordless', the proof is one sign-in by email link. A magic-link signup is proven from the start, so an unproven account exists only when something opened a session without that link: an app signing in the account a guest purchase created, or staff marking an address unverified.
Until then the account can sign in and use the dashboard, but these answer 403
with data.reason: 'address-unverified':
POST /api/payments/checkout- adding a passkey, starting TOTP enrolment and connecting an OAuth provider
POST /api/orgs/:org/invitations,PATCH /api/orgs/:org/members/:userId, and accepting an invitation sent to another address (Tenancy)
The dashboard keeps a warning toast up on every page of the dashboard layout.
Under password its button resends the confirmation link. Under
passwordless it reads Email me a link and posts to
POST /api/auth/magic/confirm, which mails the session's own address a sign-in
link that lands back on the page; the button comes back as Send another link a
minute later. Both are paced at one mail a minute. OAuth accounts start
verified.
When an account that has never proven an address proves one, by the verify
link, a completed password reset or a sign-in by email link, every passkey,
TOTP factor and connected provider it holds is deleted if it has a password. A
reset first gives one to an account that never had one.
The first sign-in by email link to such an account also signs every other
session out, records auth.otherSessionsRevoked in its activity, and cancels
a pending address change. To refuse a new way in on a route of your own, start
it with requireProvenAddress(event) from
#layers/kit/server/auth/address-proof. It is requireVerifiedSession plus the
same 403. Outside auth, call awaitsAddressProof(session.user) from
#layers/kit/shared/auth/address-proof.
Second factors and passkeys
TOTP enrolment is on Settings → Security. Confirming the first code issues 10 recovery codes of 16 characters, shown once. A recovery code signs in when the authenticator is lost, and generating a new set replaces the old one.
Adding or removing a passkey needs a fresh proof on the same device: a sign-in
or a re-authentication within the last 10 minutes. Without one the route
answers 403 with data.reason: 'proof-required', and the settings card asks
for the password or a passkey (POST /api/auth/reauth,
POST /api/auth/reauth/passkey) and retries. Put a route of yours behind the
same check with requireRecentProof(event, session) from
#layers/kit/server/auth/reauth. Every new passkey mails the owner.
Changing the password, changing the email address and deleting the account ask for the current password when the account has one.
An account can't remove its last way in. The DELETE routes for passkeys and
connected providers answer 409 with data.reason: 'last-sign-in-method' when
nothing would be left. A removal route of your own calls
assertStillReachableIn(tx, userId, { kind: 'passkey', id }) inside a
serializable transaction before it deletes, as those routes do.
Sessions
A session lives until it goes 30 days without a request, its cookie turns 30 days old, or something revokes it. The cookie's 30 days start again each time it is sealed, such as at a sign-in, a completed second factor, a password change, signing out other sessions, or a profile or photo save.
Settings → Security lists the account's devices and ends one or all the others
(GET /api/auth/sessions, DELETE /api/auth/sessions/:id,
DELETE /api/auth/sessions).
To sign an account out everywhere, bump its version:
await revokeSessions(event, userId)
To keep the device making the request, keep its row and reseal its cookie:
await revokeSessions(event, session.user.id, { keep: session.sid })
await issueVerifiedSession(event, session.user.id)
Password changes and resets, confirmed email changes and account deletion
already revoke sessions. A version bump also voids a pending address change:
its confirm link answers 400 link-invalid once the account's
session_version has moved past the one it was asked under, or once the
session that asked is signed out. When you change a users column the session
carries, such as the name or role, call issueVerifiedSession too, so the copy
sealed in the cookie matches.
Rate limits and Turnstile
Every credential check reserves an attempt before it runs. The limits are
POLICY in
layers/kit/server/auth/throttle.ts(Licensees only):
| Scope | Keyed by | Allows |
|---|---|---|
password | IP and address | 10 per 15 minutes |
totp, totp-recovery, reauth | Account | 5 per 15 minutes each |
totp-daily | Account | 20 per day |
passkey | IP | 30 per 15 minutes |
register | Address | 10 per hour |
magic-link, password-reset, email-verify | Address | 10 per hour, 1 per minute |
magic-confirm | Account | 3 per hour, 1 per minute |
email-change | New address | 10 per hour, 1 per minute |
email-change-account | Account | 3 per hour, 1 per minute |
A spent scope locks for 15 minutes, magic-confirm and email-change-account
for an hour. POST /api/auth/magic/confirm spends magic-confirm and then the
address's magic-link, and hands the account's send back when the address
refuses. POST /api/auth/email/change spends email-change-account and then
the new address's email-change, and hands the account's request back when the
address refuses. A passkey sign-in that succeeds hands its attempts back, so
only abandoned and failed ceremonies count. The counters live in the
AUTH_THROTTLE Durable Object, and the AUTH_RATE_LIMIT binding (20 requests
a minute) sheds bursts in front of them. Both are declared in
wrangler.jsonc(Licensees only).
Staff holding users:manage can lift a customer's lockout from
/dashboard/customers (POST /api/admin/users/unlock). It clears the
counters keyed to the account and its address, and the password counters of
IPs that failed against it in the last 15 minutes. The per-IP passkey counter
and the new address's email-change counter stay.
Turnstile guards password sign-in, registration, reset and magic-link requests,
guest checkout and the contact page. The server checks it only when both
NUXT_PUBLIC_TURNSTILE_SITE_KEY and NUXT_TURNSTILE_SECRET_KEY are set, and
outside pnpm dev the contact page refuses until they are
(Contact).
pnpm dev runs on Cloudflare's always-pass test keys.
Delete an account
Settings → Profile → Delete account calls DELETE /api/auth/account with the
typed email address, plus the current password when the account has one.
account.deletionGraceDays decides what happens next:
- A number (30 as shipped): the kit mails a restore link, deletes every session
row and refuses sign-in with 403
sign-in-deletion-scheduled. Until the window closes the link opens/auth/restore-account, which callsPOST /api/account/restore. After it, theaccount:purgetask erases the account onschedule.cleanup(daily by default). false: the account is erased in the request.
The restore mail goes out before anything changes, so a failed send fails the
request and leaves the account as it was. The build refuses a grace period
with email.enabled: false. With organizations on, the request answers 409
sole-owner while the account is the only owner of an organization other
people still belong to (Tenancy).
purgeAccount in
layers/kit/server/auth/deletion.ts(Licensees only)
erases. Deleting the users row cascades to credentials, sessions, tokens,
memberships and notifications. The account's photo is deleted from the bucket,
and so is the photo of every organization it leaves empty, including a branch
under such an organization that nobody else is in. Payment records stay with
user_id set to null and the stored provider payload redacted. Any table of
yours with a user column references users with a cascade, in
server/database/schema.ts:
import { integer, pgTable, text } from 'drizzle-orm/pg-core'
import { users } from '#layers/kit/server/database/schema'
export const acmeNotes = pgTable('acme_notes', {
id: integer('id').primaryKey().generatedByDefaultAsIdentity(),
userId: integer('user_id')
.notNull()
.references(() => users.id, { onDelete: 'cascade' }),
body: text('body').notNull(),
})
Settings → Profile also has Download my data (GET /api/auth/export), a JSON
file of the profile with its mail and notification opt-outs, the organizations
the account belongs to with its role in each, the invitations addressed to it,
its notifications, connected providers, passkey labels, accepted terms
versions, entitlements, payment events, sessions and activity. The file is
built by
layers/kit/server/auth/routes/data-export.get.ts(Licensees only),
which calls the account:export hook before it answers. Your tables join the
file under app from a nitro plugin of yours:
// server/plugins/account-export.ts
export default defineNitroPlugin((nitro) => {
nitro.hooks.hook('account:export', async ({ event, userId, data }) => {
data.notes = await useDrizzle(event)
.select({ body: tables.acmeNotes.body })
.from(tables.acmeNotes)
.where(eq(tables.acmeNotes.userId, userId))
})
})
Track terms acceptance
Set legal.termsVersion to any string. A date reads best:
legal: {
termsVersion: '2026-09-01',
},
Submitting the register form then records acceptance, and every signed-in user
whose recorded version differs gets a dialog to accept the current terms
(POST /api/auth/terms/accept) on any page outside /legal/. The dialog stays
shut on every page under /legal/, yours included, in every language, so its
own links open the terms and the privacy policy unobstructed. Magic-link and
OAuth signups meet the dialog on their first page. Any change to the string
prompts everyone again, so change it only for material edits. Acceptances land
in users.tos_accepted_version and one tos_acceptances row per version. The
pages themselves are content/<locale>/legal/. With false, the shipped
value, none of this is built.
React to auth events
| Event | Fires when | Payload |
|---|---|---|
auth:registered | An account is created | userId, email, method, verified |
auth:emailVerified | An account proves its address | userId, email |
auth:emailChanged | A new address lands on the account | userId, email, previousEmail |
account:deletionScheduled | Deletion is requested | userId, purgeAt, subscriptions |
account:subscriptionsStranded | The purge closed an organization that is still billed | userId, subscriptions |
account:purged | The account has been erased | userId |
A password signup fires auth:registered with verified: false, then
auth:emailVerified once the link is opened or a password reset completes.
Magic-link and OAuth signups arrive verified. Provision anything real only for
a proven address. This
server/listeners/welcome.ts queues the crm-sync job from
Background jobs:
import { defineListener } from '#layers/kit/server/events/types'
import { enqueue } from '#layers/kit/server/utils/queue'
export default defineListener({
event: 'auth:registered',
async handle({ userId, verified }, { event }) {
if (!verified) return
await enqueue(event, { kind: 'crm-sync', payload: { userId } })
},
})
Add a second listener on auth:emailVerified wherever an account can exist
unproven: password signups, guest-checkout accounts and accounts staff marked
unverified (Events). A guest-checkout account is proven by its
first sign-in by email link under passwordless, and under password by the
reset that sets its first password.
Turn auth off
auth.enabled: false mounts no auth route, and the auth page middleware lets
everyone through, so the dashboard is open and has no account menu. The build
refuses payments, organizations and notifications while auth is off, since each
needs a signed-in user.
Configuration
| Key | Default | What it does |
|---|---|---|
auth.enabled | true | Off mounts no auth route and opens the dashboard. |
auth.primary | 'password' | 'password', 'passwordless' or 'none'. |
auth.oauth | [] | Any of 'github', 'google', 'microsoft', or a provider you dropped in. |
auth.adapters | none | Declarations for a provider in server/auth/adapters/: its label, icon and emailTrust, all required, and any credentials. On a shipped provider whose handler you didn't replace, only credentials. |
auth.totp | true | Authenticator codes as a second factor. |
auth.passkey | 'login' | 'login', 'second-factor' or false. |
auth.emailVerification | true | Password signups confirm their address first. Ignored under 'passwordless'. |
account.deletionGraceDays | 30 | Days a deletion can be undone. false erases at once. |
account.avatarUpload | true | Off unmounts the photo routes and hides the control. Stored photos stay served (Storage). |
legal.termsVersion | false | The current terms version. Changing it prompts everyone again. |
access.firstAccountRole | 'superadmin' | Role of the first account to prove its address (Permissions). |
| Variable | What it does |
|---|---|
NUXT_SESSION_PASSWORD | Cookie encryption key, 32+ characters. pnpm dev writes one to .env. |
NUXT_SESSION_COOKIE_SECURE | Set false only for a production build served over plain http. |
NUXT_SITE_URL | Origin of every mailed link. Outside pnpm dev, sending a link fails without it. |
NUXT_OAUTH_<PROVIDER>_CLIENT_ID, NUXT_OAUTH_<PROVIDER>_CLIENT_SECRET | One pair per shipped provider in auth.oauth. |
NUXT_OAUTH_<PROVIDER>_<KEY> | One per key in auth.adapters.<id>.credentials. A dropped-in provider that declares none reads one JSON-valued NUXT_OAUTH_<PROVIDER>. |
NUXT_OAUTH_MICROSOFT_TENANT | Required with Microsoft. The [env] warning names it when unset. |
NUXT_PUBLIC_TURNSTILE_SITE_KEY, NUXT_TURNSTILE_SECRET_KEY | Turn on the Turnstile check. Both or neither. |
Good to know
pnpm devhas no Durable Objects, so every auth limit is off there and a[rate-limit]warning is logged. Try lockouts underpnpm preview.- With
emailVerificationoff, someone can register a victim's address first, add a passkey or TOTP, and keep that way in after the owner resets the password. Leave it on underprimary: 'password'. - A password reset creates a first password only for an account that has never
proven an address, such as one guest checkout created. Before you drop a
provider from
auth.oauth, check for accounts that only ever signed in through it: under'password'they are locked out. - Mailed links use
NUXT_SITE_URL, never the request's host. Set it at build, orNUXT_PUBLIC_SITE_URLon the Worker. - A column of yours that references
users, or is nameduser_id, withoutonDelete: 'cascade'failslayers/kit/test/unit/schema-cascades.test.ts(Licensees only), which readsserver/database/schema.tstoo. A column that sets null or keeps no foreign key on purpose is listed intest/helpers/schema-cascades.ts. - An app route for a listed provider fails the build:
server/api/auth/oauth/<id>.ts,<id>.get.ts,<id>/index.tsor<id>/index.get.ts, or the same underserver/routes/api/. A dynamic[provider]file does not, because the kit's static route wins. - A dropped-in handler is mounted for
GETonly, so a provider that returns byform_post, such as Apple, can't finish. - The build checks only that an
iconis shaped like an Iconify name. One from a collection the app hasn't installed renders nothing or is fetched from the Iconify API. The kit installs@iconify-json/fa6-brandsand@iconify-json/lucide.
A link with a lock opens the kit’s private repository, which every licensee can read.