twoFactor.requireSetup (on by default with 2FA): an account of the 2FA collection that has not set TOTP up is refused by ipal-kit's second-factor check on every collection, global and plugin endpoint; the setup screen and /api/setup-totp are all it reaches. It has 72 hours from its creation or from an admin's reset (field totpSetupFrom, stamped by a hook); after that setup is refused too until an admin reopens it. Accounts that existed before get their window at the first start after the upgrade (backfill in onInit), so nobody is blocked when a site upgrades. Admin side: status and reset / 'Reopen setup' button on the account page (TwoFactorStatusField), POST /api/ipal/two-factor/reset, a dashboard notice (TwoFactorAlert), a notice on the setup screen; texts in English and Polish. ipal-doctor reports blocked accounts and a switched-off mode. Overridable: requireSetup false, windowHours (false / 0 = none), exempt for a service account, resetAccess; the per-entity second-factor opt-out applies. API keys, seeds, bearer-token routes and sites with twoFactor false are not affected. HTTP tests (REST, GraphQL, first admin on an empty database) run on SQLite, Postgres and MongoDB; a browser test on the reference site covers the first admin, a new editor and the admin's reset. Docs, guide, skills, changelog. Co-Authored-By: Claude Fable 5.2 <[email protected]>
53 lines
1.9 KiB
TypeScript
53 lines
1.9 KiB
TypeScript
'use client'
|
|
|
|
import { toast, useConfig, useTranslation } from '@payloadcms/ui'
|
|
import { useState } from 'react'
|
|
|
|
import { ipalT } from '../../admin-i18n/t.js'
|
|
|
|
type Props = {
|
|
/** `reopen`: the account has no TOTP (window passed or still open). `reset`: it has one (lost phone). */
|
|
kind: 'reopen' | 'reset'
|
|
userId: number | string
|
|
}
|
|
|
|
/** The admin's button on an account: clears its TOTP so it can (or must) set it up again. */
|
|
export function TwoFactorResetButton({ kind, userId }: Props) {
|
|
const { t } = useTranslation()
|
|
const text = (key: Parameters<typeof ipalT>[1]) => ipalT(t as never, key)
|
|
const { config } = useConfig()
|
|
const [busy, setBusy] = useState(false)
|
|
|
|
const run = async () => {
|
|
if (kind === 'reset' && !window.confirm(text('twoFactorResetConfirm'))) {return}
|
|
setBusy(true)
|
|
try {
|
|
const res = await fetch(`${config.serverURL ?? ''}${config.routes?.api ?? '/api'}/ipal/two-factor/reset`, {
|
|
body: JSON.stringify({ userId }),
|
|
credentials: 'include',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
method: 'POST',
|
|
})
|
|
const data = (await res.json()) as { message?: string; ok?: boolean }
|
|
if (data.ok) {
|
|
toast.success(data.message ?? text('twoFactorResetDone'))
|
|
// The status next to this button is rendered on the server with the document: load it again.
|
|
// (router.refresh() does not re-render a document's field components.)
|
|
window.location.reload()
|
|
} else {
|
|
toast.error(data.message ?? text('twoFactorResetFailed'))
|
|
}
|
|
} catch {
|
|
toast.error(text('twoFactorResetFailed'))
|
|
} finally {
|
|
setBusy(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<button className="btn btn--style-secondary btn--size-small" disabled={busy} onClick={run} style={{ marginTop: '.5rem' }} type="button">
|
|
{text(kind === 'reopen' ? 'twoFactorReopen' : 'twoFactorReset')}
|
|
</button>
|
|
)
|
|
}
|