Files
ipal-kit/src/modules/security/totp/TwoFactorResetButton.tsx
T
Radosław SmolińskiandClaude Fable 5.2 0a4f001797 IAPL rename, scripted: preferred names in src, bin, scripts, services, tests; bin files and skill folders moved
Replaced by an explicit list of forms only (ten exported names, runtime env names, project file
names and marks, commands, skill names, the log prefix, the brand in prose). Package name, import
paths, routes, CSS tokens, data attributes, custom keys, context flags, the cookie and kv keys are
not touched. package.json keeps the five old bin names beside the five new ones.
The old names stop working in this commit and come back as aliases in the next; docs follow.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-25 17:24:21 +01:00

53 lines
1.9 KiB
TypeScript

'use client'
import { toast, useConfig, useTranslation } from '@payloadcms/ui'
import { useState } from 'react'
import { iaplT } 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 iaplT>[1]) => iaplT(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>
)
}