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 /** 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 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 = { ...ipalTotpClientProps, back, searchParams } for (const key of CLIENT_PROPS) { if (key in rest) {clientProps[key] = (rest as Record)[key]} } return RenderServerComponent({ clientProps, Component: ipalTotpView, importMap: props.importMap, serverProps: { ...rest, searchParams }, }) }