@frogbotai/plugin-two-factor
v0.43.0
Published
Two-factor sign-in for FrogBot: authenticator codes and recovery codes after the password or sign-in provider.
Readme
@frogbotai/plugin-two-factor
Two-factor sign-in for FrogBot. After the password or a sign-in provider, a user with it on enters a code from an authenticator app or a recovery code before they get a session. No session exists until the code is accepted.
Full guide: Two-factor sign-in.
Setup
pnpm add @frogbotai/plugin-two-factorimport { twoFactor } from '@frogbotai/plugin-two-factor';
import { buildConfig } from 'frogbot';
export default buildConfig({
secret: process.env.FROGBOT_SECRET!,
db: databaseAdapter,
collections: [{ slug: 'users', auth: true, fields: [{ name: 'role', type: 'text' }] }],
plugins: [
twoFactor({
issuer: 'Acme',
require: ({ user }) => user.role === 'admin',
canReset: ({ req, user }) => req.user?.role === 'admin' && user.role !== 'owner',
}),
],
});Run frogbot generate:importmap in build-only workflows after adding the plugin; frogbot dev does it for you.
The plugin adds twoFactorEnabled (read-only, with the account section) and three hidden fields to the auth collection: the TOTP secret, encrypted with connections.encryption; the recovery codes, hashed; and the last accepted time step.
Options
| Option | Default | Description |
| -------------------------- | ------------ | ---------------------------------------------------------------------------------------------------------------------------------- |
| collection | admin.user | Auth collection that gets two-factor sign-in. |
| require({ req, user }) | nobody | Users who must have it: setup at the next sign-in, and they can't turn it off. |
| canEnable({ req, user }) | everyone | Users who may turn it on; false gives 403. |
| canReset({ req, user }) | nobody | Whether the signed-in user may reset user's. Asked only for a user they can read and update. |
| issuer | 'FrogBot' | Name authenticator apps show. |
| emails | built-in | Replace an event's email (enabled, disabled, recoveryCodeUsed, recoveryCodesCreated, reset) or turn it off with false. |
| keysURL | none | Where users manage API keys; the account section's "API keys aren't affected" note links to it. |
Turning it on
The user's account section shows a QR code and the key as text, asks for a current code, then shows 10 recovery codes once with copy and download. Turning it on ends every other session of that user (with auth.useSessions: false, issued tokens stay valid until they expire). New recovery codes needs a code and voids the old set; Turn off needs a code or a recovery code. API keys aren't affected.
Codes are RFC 6238 (SHA-1, 30 s, one step of drift), each usable once. Five wrong codes in 10 minutes lock code entry for that user for 10 minutes.
Require
A required user without two-factor sign-in goes to setup at their next sign-in, before any session exists, including the first user at registration. While require returns true, Turn off is hidden and POST /two-factor/disable answers 403.
Recovery and reset
A recovery code signs in once. After using one, the code page says how many are left and, at 3 or fewer, advises making new ones, with a link to the account page for the admin's user collection only (other auth collections get the advice without a link); JSON clients get recoveryCodesLeft. When canReset allows it, another user's document shows Reset two-factor sign-in: it removes their secret and codes, ends every session (with auth.useSessions: false, issued tokens stay valid until they expire) and clears the lock. canReset is asked only for a user the admin can read and update. Nobody can reset their own.
Sessions and password login
- Sign-in methods pause in the collection's
beforeSignInhook and redirect to the code page at/api/<collection>/two-factor. - The admin's login view is replaced (unless the app sets
admin.components.views.login) by one with the same slots whose form follows the code step. POST /api/<collection>/loginanswers202 { message, twoFactor: 'code' | 'setup', url }with no token. Post{ code }to<url>/verifywith the returned cookie for the normal login response. AreturnTopath in the login body makes the code page continue there.POST /api/<collection>/reset-passwordsaves the password and ends every session, but signs nobody in who needs a code:{ message, twoFactor: 'signIn' }. GraphQL and the Local APIresetPasswordsave it too and return no token.
The paused sign-in is a 10-minute, single-use KV record behind an HttpOnly, SameSite=Lax cookie scoped to the code endpoints. It grants no access on its own.
Limits
- GraphQL
loginUserand the Local APIloginhave no code step and fail for users who need a code or setup. canResetdefaults to nobody: without it, a user who loses their authenticator and recovery codes can't get back in.- Another pausing
beforeSignInhook (a terms step, say) can't be combined yet: resuming it withfinishSignInfor a user who needs a code is refused with403. - No passkeys, security keys or per-tenant settings.
