Fixed 2fa and white-label

This commit is contained in:
2026-10-12 13:10:34 +02:00
parent 13d669b757
commit 78e2f8fc7f
14 changed files with 322 additions and 161 deletions
+41 -22
View File
@@ -62,29 +62,35 @@ import { buildSeoPlugin, injectAutoFillMeta, injectSeoTabs } from './modules/seo
if (!tf?.issuer) {
throw new Error('[ipal] twoFactor.issuer is required (name shown in the authenticator ' + 'app). Pass twoFactor: { issuer: "Your Site" }, or twoFactor: false to ' + 'opt out (discouraged).');
}
try {
// Dynamic specifier via a variable so TS doesn't try to resolve this
// optional peer dependency at build time (it isn't in the plugin's own
// node_modules). Avoids TS2307 without @ts-expect-error; the module
// exists at runtime in projects that installed it.
// @ts-ignore
const mod = await import('@clocklimited/payload-2fa');
// The package exports `payloadTotp`; older/other builds may use
// `totpPlugin`. Accept either so a rename doesn't break us.
const totp = mod.payloadTotp ?? mod.totpPlugin;
if (typeof totp !== 'function') {
throw new Error('expected export payloadTotp (or totpPlugin) to be a function — ' + 'check the installed @clocklimited/payload-2fa version');
// Prefer the INJECTED plugin factory (recommended): the project imports
// payloadTotp from @clocklimited/payload-2fa and passes it in. Then the
// plugin never imports the optional peer itself — no dynamic import, so
// Next can't fail to bundle it (the cause of "Cannot find module
// '@clocklimited/payload-2fa'" crashing even public frontend pages).
let totp = 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).
try {
const pkg = '@clocklimited/payload-2fa';
const mod = await import(pkg);
totp = mod.payloadTotp ?? mod.totpPlugin;
} 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 " + `twoFactor: false to opt out (discouraged). Original error: ${String(err)}`);
}
config = await totp({
collection: tf.collectionSlug ?? 'users',
forceSetup: true,
totp: {
issuer: tf.issuer
}
})(config);
} catch (err) {
throw new Error('[ipal] 2FA is enforced but @clocklimited/payload-2fa is not installed. ' + 'Run: pnpm add @clocklimited/payload-2fa — or set twoFactor: false to ' + `opt out (discouraged). Original error: ${String(err)}`);
}
if (typeof totp !== 'function') {
throw new Error('[ipal] 2FA: expected payloadTotp (or totpPlugin) to be a function — check the ' + 'injected `plugin` or the installed @clocklimited/payload-2fa version.');
}
config = await totp({
collection: tf.collectionSlug ?? 'users',
forceSetup: true,
totp: {
issuer: tf.issuer
}
})(config);
}
// --- white-label admin branding ---
// Inject admin.components.graphics.Logo/Icon pointing at the plugin's
@@ -139,11 +145,24 @@ import { buildSeoPlugin, injectAutoFillMeta, injectSeoTabs } from './modules/seo
}
},
...creditOn ? {
afterLogin: [
...config.admin?.components?.afterLogin ?? [],
{
path: '@intecion/ipal-kit/admin#IntecionCredit',
serverProps: {
...creditProps,
variant: 'login'
}
}
],
afterNavLinks: [
...config.admin?.components?.afterNavLinks ?? [],
{
path: '@intecion/ipal-kit/admin#IntecionCredit',
serverProps: creditProps
serverProps: {
...creditProps,
variant: 'nav'
}
}
]
} : {}