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]>
This commit is contained in:
Radosław Smoliński
2026-11-24 16:16:39 +01:00
co-authored by Claude Fable 5.2
parent 93e75426ab
commit ad51456eaa
21 changed files with 477 additions and 70 deletions
@@ -0,0 +1,49 @@
/**
* Is the project's committed admin import map older than the config?
*
* ipalKit registers `SafeTotpView` in front of the 2FA screens. Payload resolves view components
* through the import map file, and a path that is not in it renders NOTHING: the 2FA screen comes
* back empty (HTTP 200, no code form), so nobody can sign in — after an upgrade where
* `pnpm payload generate:importmap` was forgotten. In development Payload regenerates the file
* by itself; a production build uses the committed one. This check makes that state loud: an
* error in the server log at start, and a message on the 2FA screens (TotpRedirectProvider).
* `twoFactor.importMapCheck: false` turns it off.
*/
type ConfigLike = { admin?: { importMap?: { baseDir?: string; importMapFile?: string } }; routes?: { admin?: string } }
/** The file Payload reads the import map from — the same places Payload itself looks in. Null when not found. */
export async function findImportMapFile(config: ConfigLike, cwd = process.cwd()): Promise<null | string> {
try {
const fs = await import('node:fs/promises')
const path = await import('node:path')
const exists = (file: string) => fs.access(file).then(() => true, () => false)
const explicit = config.admin?.importMap?.importMapFile
if (explicit) {return (await exists(explicit)) ? explicit : null}
const rootDir = config.admin?.importMap?.baseDir ?? cwd
// Next.js writes %5F for route segments that begin with an underscore
const adminFolder = (config.routes?.admin ?? '/admin').replace(/(^|\/)_/g, '$1%5F')
for (const base of [`app/(payload)${adminFolder}`, `src/app/(payload)${adminFolder}`]) {
const file = path.resolve(rootDir, base, 'importMap.js')
if (await exists(file)) {return file}
}
} catch {
/* no file system (or not Node): nothing to check */
}
return null
}
/**
* The entries of `needed` that the import map file lacks. An empty list also when the file is
* not found or cannot be read: an unknown state is never reported as a problem.
*/
export async function missingImportMapEntries(config: ConfigLike, needed: string[], cwd?: string): Promise<{ file: null | string; missing: string[] }> {
const file = await findImportMapFile(config, cwd)
if (!file) {return { file: null, missing: [] }}
try {
const fs = await import('node:fs/promises')
const text = await fs.readFile(file, 'utf8')
return { file, missing: needed.filter((entry) => !text.includes(`"${entry}"`) && !text.includes(`'${entry}'`)) }
} catch {
return { file, missing: [] }
}
}