UNPKG

payload-totp

Version:

Add an extra security layer to PayloadCMS using a Time-based One-time Password (TOTP).

40 lines (39 loc) 1.75 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { formatAdminURL } from '@payloadcms/ui/shared'; import { headers } from 'next/headers.js'; import { redirect } from 'next/navigation.js'; import { normalizePathname } from '../../utilities/normalizePathname.js'; import TOTPProviderClient from './index.client.js'; export const TOTPProvider = async (args)=>{ const { children, payload, pluginOptions, user: _user } = args; const user = _user; const headersList = await headers(); const pathname = headersList.get('x-pathname') || '/'; const verifyUrl = formatAdminURL({ adminRoute: payload.config.routes.admin, path: '/verify-totp' }); const setupUrl = formatAdminURL({ adminRoute: payload.config.routes.admin, path: '/setup-totp' }); const normalizedPathname = normalizePathname(pathname); const normalizedVerifyUrl = normalizePathname(verifyUrl); const normalizedSetupUrl = normalizePathname(setupUrl); if (user && user.hasTotp && ![ 'api-key', 'totp' ].includes(user._strategy) && normalizedPathname !== normalizedVerifyUrl) { redirect(`${payload.config.serverURL}${verifyUrl}?back=${encodeURIComponent(pathname)}`); } else if (user && !user.hasTotp && pluginOptions.forceSetup && normalizedPathname !== normalizedSetupUrl && user._strategy !== 'api-key') { redirect(`${payload.config.serverURL}${setupUrl}?back=${encodeURIComponent(pathname)}`); } else { return /*#__PURE__*/ _jsx(TOTPProviderClient, { forceSetup: pluginOptions.forceSetup, setupUrl: setupUrl, verifyUrl: verifyUrl, children: children }); } }; //# sourceMappingURL=index.js.map