UNPKG

payload-totp

Version:

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

31 lines (30 loc) 1.34 kB
/* eslint-disable no-restricted-exports */ 'use client'; import { useAuth } from '@payloadcms/ui'; import { usePathname, useRouter } from 'next/navigation.js'; import { useEffect } from 'react'; import { normalizePathname } from '../../utilities/normalizePathname.js'; export default function TOTPProviderClient(args) { const { children, forceSetup, setupUrl, verifyUrl } = args; const { user } = useAuth(); const strategy = user?._strategy; const router = useRouter(); const pathname = usePathname(); const normalizedPathname = normalizePathname(pathname); const normalizedVerifyUrl = normalizePathname(verifyUrl); const normalizedSetupUrl = normalizePathname(setupUrl); useEffect(()=>{ if (user && user.hasTotp && strategy && ![ 'api-key', 'totp' ].includes(strategy) && normalizedPathname !== normalizedVerifyUrl) { router.push(`${verifyUrl}?back=${encodeURIComponent(pathname)}`); } else if (user && !user.hasTotp && forceSetup && normalizedPathname !== normalizedSetupUrl && strategy !== 'api-key') { router.push(`${setupUrl}?back=${encodeURIComponent(pathname)}`); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [ user ]); return children; } //# sourceMappingURL=index.client.js.map