Files
ipal-kit/src/modules/security/totp/SafeTotpView.tsx
T
Radosław SmolińskiandClaude Fable 5.2 ad51456eaa 2FA back link: review fixes — views guarded by object, client views, a loud stale import map
SB-1: the 2FA package replaces a view that already stands under its keys, so a
key that existed before the factory ran was wrongly treated as the project's own
and left unguarded (also with a double registration, or ipalKit over its own
output). Views are now compared by object, right after the factory.

SB-2: SafeTotpView renders through Payload's RenderServerComponent: a client
component set as a 2FA view gets only client props plus the checked back.

SB-3: a production build with the previous import map shows an empty 2FA screen.
twoFactor.importMapCheck (on, production only) reads the committed map at start:
an error in the log and a message on the 2FA screens. Docs name the real symptom.

SB-4: enabled: false restores admin.dependencies; wrapTotpViews keeps exportName.

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

71 lines
3.3 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 { safeAdminBack, type SafeBackRule, type TotpViewComponent, totpViewImportKey } from './safeBack.js'
export type SafeTotpViewProps = {
/** `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.
*
* ipalKit 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 { 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(
`[ipal] The admin import map has no "${totpViewImportKey(ipalTotpView)}" (the 2FA screen ipal-kit 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]}
}
return RenderServerComponent({
clientProps,
Component: ipalTotpView,
importMap: props.importMap,
serverProps: { ...rest, searchParams },
})
}