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