2FA fixed

This commit is contained in:
2026-10-15 22:24:28 +02:00
parent eb436528d4
commit bd2fbaf8bf
16 changed files with 808 additions and 208 deletions
+8 -7
View File
@@ -78,10 +78,11 @@ export const ipalKit = (options: IpalOptions): Plugin => {
let totp: TotpPluginFactory | undefined = tf.plugin
if (!totp) {
// Fallback: dynamic import. Variable specifier dodges the plugin's own
// TS2307, but Next may NOT trace it into the server bundle — projects
// using this path MUST add '@clocklimited/payload-2fa' to
// serverExternalPackages in next.config (see docs/security.md).
// Fallback: dynamic import, resolvable because the package is an optional
// peer. Variable specifier dodges the plugin's own TS2307. Works only
// outside Next (payload CLI): webpack turns it into an empty context, and
// serverExternalPackages fails on the package's own .css imports — in
// Next the project MUST inject `plugin` (see docs/security.md).
try {
const pkg = '@clocklimited/payload-2fa'
const mod = (await import(pkg)) as {
@@ -92,9 +93,9 @@ export const ipalKit = (options: IpalOptions): Plugin => {
} catch (err) {
throw new Error(
'[ipal] 2FA is enforced but @clocklimited/payload-2fa could not be loaded. ' +
'Best fix — inject it: `import { payloadTotp } from "@clocklimited/payload-2fa"` ' +
'then `twoFactor: { issuer, plugin: payloadTotp }`. Or install it AND add ' +
"'@clocklimited/payload-2fa' to serverExternalPackages in next.config. Or set " +
'Inject it (required in Next): `import { payloadTotp } from "@clocklimited/payload-2fa"` ' +
'then `twoFactor: { issuer, plugin: payloadTotp }`. Adding it to ' +
'serverExternalPackages does NOT help (the package imports .css). Or set ' +
`twoFactor: false to opt out (discouraged). Original error: ${String(err)}`,
)
}
+15 -12
View File
@@ -1,11 +1,14 @@
import type { Field, Plugin } from 'payload'
import type { CollectionSlug, Field, Plugin } from 'payload'
/**
* Factory shape of @clocklimited/payload-2fa's `payloadTotp` export — injected
* so the plugin never imports the optional peer itself.
* so the plugin never imports the optional peer itself. `collection` is
* `CollectionSlug` (not `string`): payloadTotp requires it, and in a project
* with generated payload-types it's a narrowed union — `string` would make
* `plugin: payloadTotp` fail to type-check.
*/
export type TotpPluginFactory = (options: {
collection: string
collection: CollectionSlug
forceSetup: boolean
totp: { issuer: string }
}) => Plugin
@@ -90,19 +93,19 @@ export type IpalOptions = {
twoFactor?:
| {
/** Auth collection slug. Defaults to 'users'. */
collectionSlug?: string
collectionSlug?: CollectionSlug
/** Name shown in the authenticator app (e.g. company/site name). */
issuer: string
/**
* The 2FA plugin factory, INJECTED from the project:
* `import { payloadTotp } from '@clocklimited/payload-2fa'`. Strongly
* recommended — injecting it (the same pattern as NextResponse /
* revalidatePath) means the plugin never dynamically imports the
* optional peer, which Next can fail to bundle into the server chunks
* ("Cannot find module '@clocklimited/payload-2fa'" at runtime, even on
* public frontend pages). If omitted, the plugin falls back to a dynamic
* import and you MUST add '@clocklimited/payload-2fa' to
* serverExternalPackages in next.config. See docs/security.md.
* `import { payloadTotp } from '@clocklimited/payload-2fa'`. REQUIRED in
* Next — injecting it (the same pattern as NextResponse / revalidatePath)
* means the plugin never dynamically imports the optional peer, which
* Next can't bundle ("Cannot find module '@clocklimited/payload-2fa'",
* even on public frontend pages). serverExternalPackages is no fix: the
* package imports its own .css, which Node can't load. If omitted, the
* plugin falls back to a dynamic import — works only outside Next
* (payload CLI). See docs/security.md.
*/
plugin?: TotpPluginFactory
}