Files
ipal-kit/src/modules/security/totp/SafeTotpView.tsx
T
Radosław SmolińskiandClaude Fable 5.2 b6c91a3a11 Sign-in look: the client's logo or the site's name on a dark panel beside the form (whiteLabel.signIn)
On by default with whiteLabel. The login screen and the 2FA screens get the look through AdminLogo and
SafeTotpView, which every site already has in its import map: an upgrade needs no edit. allScreens adds one
provider (SignInLook) for forgot / reset password, first user and logout; a production start leaves it out
when the import map on disk lacks it. Logo and name come from Site settings: no new field, no migration.
Language links through GET /api/ipal/admin-language (Payload's own cookie, back to an admin path only).
Options: accent, logoMadeFor, languages, svgLogo; whiteLabel.brandAccess 'admin'. The 2FA code boxes get
names for screen readers. Reference site and starter: allScreens on. Docs, agent guide, skills, tests.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-26 20:35:33 +01:00

104 lines
4.8 KiB
TypeScript

import type { AdminViewServerProps } from 'payload'
import type React from 'react'
import { RenderServerComponent } from '@payloadcms/ui/elements/RenderServerComponent'
import { getFromImportMap } from 'payload/shared'
import type { SignInLookProps } from '../../branding/signIn/brand.js'
import { iaplT } from '../../admin-i18n/t.js'
import { securityTexts } from '../../admin-i18n/texts/security.js'
import { signInProviderActive } from '../../branding/signIn/brand.js'
import { SignInPanel } from '../../branding/signIn/SignInPanel.js'
import { CodeInputNames } from './CodeInputNames.js'
import { safeAdminBack, type SafeBackRule, type TotpViewComponent, totpViewImportKey } from './safeBack.js'
export type SafeTotpViewProps = {
/**
* `whiteLabel.signIn`, set by the plugin: the sign-in panel is printed in front of the 2FA screen when the
* provider (`SignInLook`) does not print it. Not set: the screen as the package draws it.
*/
iaplSignIn?: SignInLookProps
/** `twoFactor.safeBack` as a function: replaces the default rule. */
ipalSafeBack?: SafeBackRule
/** `clientProps` of the view's own registration; passed on to it. */
ipalTotpClientProps?: Record<string, unknown>
/** The view to render, as registered: `'@clocklimited/payload-2fa/rsc#TOTPVerify'` or `{ path, exportName }`. */
ipalTotpView: TotpViewComponent
} & AdminViewServerProps
const originOf = (serverURL: unknown): string | undefined => {
if (typeof serverURL !== 'string' || !serverURL) {return undefined}
try {
return new URL(serverURL).origin
} catch {
return undefined
}
}
/** What Payload itself hands a client view (AdminViewClientProps): all serializable. */
const CLIENT_PROPS = ['browseByFolderSlugs', 'clientConfig', 'collectionSlug', 'docID', 'documentSubViewType', 'folderID', 'globalSlug', 'viewType'] as const
/**
* In front of the 2FA package's setup and verify views: replaces the `back` query value with a
* checked one (safeBack.ts) and renders the view with it. The package's form sends the browser
* to `back` after a valid code, so what it receives must be safe to navigate to.
*
* The view is rendered the way Payload renders any view: a server component gets every server
* prop, with `searchParams.back` checked; a client component (a project's own screen set through
* the wrapped factory) gets only what can cross to the browser — Payload's client props, its own
* `clientProps`, `searchParams` and `back`, both checked.
*
* iapl registers it (twoFactor.safeBack, on by default). A project that registers 2FA views
* itself can use it too: `wrapTotpViews`, or give it `ipalTotpView` and list that path in
* `admin.dependencies`.
*/
export function SafeTotpView(props: SafeTotpViewProps): React.ReactNode {
const { iaplSignIn, ipalSafeBack, ipalTotpClientProps, ipalTotpView, ...rest } = props
const config = props.initPageResult?.req?.payload?.config ?? props.payload?.config
const query = (props.searchParams ?? {}) as Record<string, unknown>
const back = (ipalSafeBack ?? safeAdminBack)(typeof query.back === 'string' ? query.back : undefined, {
adminRoute: config?.routes?.admin ?? '/admin',
origin: originOf(config?.serverURL),
})
const searchParams = { ...query, back }
if (!getFromImportMap({ importMap: props.importMap, PayloadComponent: ipalTotpView, schemaPath: '', silent: true })) {
throw new Error(
`[iapl] The admin import map has no "${totpViewImportKey(ipalTotpView)}" (the 2FA screen IAPL puts its back-link check in front of). ` +
'Run `pnpm payload generate:importmap` and commit the result.',
)
}
const clientProps: Record<string, unknown> = { ...ipalTotpClientProps, back, searchParams }
for (const key of CLIENT_PROPS) {
if (key in rest) {clientProps[key] = (rest as Record<string, unknown>)[key]}
}
const view = RenderServerComponent({
clientProps,
Component: ipalTotpView,
importMap: props.importMap,
serverProps: { ...rest, searchParams },
})
const req = props.initPageResult?.req
const payload = req?.payload ?? props.payload
// The look only: the panel is a sibling in front of the screen, the screen itself is rendered as above.
if (!iaplSignIn) {return view}
const i18n = (req?.i18n ?? props.i18n) as { t: (key: never, vars?: Record<string, unknown>) => string } | undefined
const digit = i18n ? iaplT(i18n.t, 'twoFactorCodeDigit', { n: '%n', total: '%t' }) : securityTexts.en.twoFactorCodeDigit.replace('{{n}}', '%n').replace('{{total}}', '%t')
return (
<>
{signInProviderActive(payload) ? null : (
<SignInPanel
i18n={i18n as never}
locale={(props.initPageResult?.locale ?? props.locale) as never}
look={iaplSignIn}
payload={payload}
user={(req?.user ?? props.user) as never}
/>
)}
{view}
<CodeInputNames label={digit} />
</>
)
}