UNPKG

@centinel/nextjs

Version:

Package designed to add Centinel Analytica functionality to Next.js applications

42 lines 1.46 kB
import { NextResponse } from 'next/server'; import { CentinelMiddleware } from './core/CentinelMiddleware'; let centinelWorker = undefined; export default async function middleware(req) { try { if (!centinelWorker) { throw new Error('Centinel worker not initialized. Use createCentinelMiddleware() to create your middleware.'); } const event = { request: req }; const centinelResult = await centinelWorker.validate(event); return centinelResult.response; } catch { return NextResponse.redirect(new URL('/block', req.url)); } } export function createCentinelMiddleware(config) { return async function centinelMiddleware(req) { try { centinelWorker ??= new CentinelMiddleware(config); const event = { request: req }; const centinelResult = await centinelWorker.validate(event); return centinelResult; } catch { return { response: NextResponse.redirect(new URL('/block', req.url)), decision: 'block', should_intercept: true, }; } }; } export function createCentinelMiddlewareFromEnv() { const config = { siteKey: process.env.CENTINEL_SITE_KEY, secretKey: process.env.CENTINEL_SECRET_KEY, }; return createCentinelMiddleware(config); } export { CentinelMiddleware }; //# sourceMappingURL=middleware.js.map