UNPKG

@auth0/nextjs-auth0

Version:
28 lines (27 loc) 1.07 kB
import { NextResponse } from "next/server.js"; export const appRouteHandlerFactory = (client) => (apiRoute) => async (req, params) => { const session = await client.getSession(); if (!session || !session.user) { return NextResponse.json({ error: "not_authenticated", description: "The user does not have an active session or is not authenticated" }, { status: 401 }); } const apiRes = await apiRoute(req, params); const nextApiRes = apiRes instanceof NextResponse ? apiRes : new NextResponse(apiRes.body, apiRes); return nextApiRes; }; export const pageRouteHandlerFactory = (client) => (apiRoute) => async (req, res) => { const session = await client.getSession(req); if (!session || !session.user) { // If the user is not authenticated, return res.status(401).json({ error: "not_authenticated", description: "The user does not have an active session or is not authenticated" }); return; } await apiRoute(req, res); };