#Vercel Middleware Error

1 messages · Page 1 of 1 (latest)

vast pumice
#
export { default } from "next-auth/middleware";
import { NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
const PATTERNS = [
  [
    new URLPattern({ pathname: "/manage/:id" }),
    ({ pathname }) => pathname.groups,
  ],
];

const params = (url) => {
  const input = url.split("?")[0];
  let result = {};

  for (const [pattern, handler] of PATTERNS) {
    const patternResult = pattern.exec(input);
    if (patternResult !== null && "pathname" in patternResult) {
      result = handler(patternResult);
      break;
    }
  }
  return result;
};

export async function middleware(req) {
  const { origin } = req.nextUrl;
  const token = await getToken({ req });

  if (req.nextUrl.pathname.startsWith("/manage")) {
    if(!token) {
      return NextResponse.redirect(`${origin}`);
    }
    const { id } = params(req.url);
    // check if there is a id in the url
    if (id) {
      const res = await fetch(
        `${process.env.API_ENDPOINT}manage/${id}/${token.userid}`,
        {
          method: "GET",
          headers: {
            Authorization: `Basic ${btoa(process.env.API_SECRET)}`,
          },
        }
      );

      if (res.status === 200) {
        return NextResponse.next();
      }
      return NextResponse.redirect(`${origin}`);
    }
  }
}

export const config = {
  matcher: ["/manage/:path*"],
};``` Code
#

Network Console