With middleware, you can “500 - Internal Server Error.” error and will not display.I’m utilizing the next@13.4.12
app directory. Here’s the structure of src/middleware.ts
.
import { MiddlewareRequest, MiddlewareResponse, type NextRequest as NetlifyNextRequest } from "@netlify/next";
import { createMiddlewareClient } from "@supabase/auth-helpers-nextjs";
import { NextResponse } from "next/server";
import { Database } from "./types/supabase";
import type { NextRequest } from "next/server";
export async function middleware(nextRequest: NextRequest) {
const req = new MiddlewareRequest(nextRequest as NetlifyNextRequest);
const nextResponse = NextResponse.next();
const res = await req.next()
const supabase = createMiddlewareClient<Database>({ req: nextRequest, res: nextResponse });
const {
data: { session },
} = await supabase.auth.getSession();
const user = session?.user;
const isSubscriptions = await supabase
.from("profiles")
.select("is_subscription")
.eq("id", user?.id);
const isSubscription = isSubscriptions.data?.[0]?.is_subscription;
if (!user && req.nextUrl.pathname !== "/sign-in") {
return MiddlewareResponse.redirect(new URL("/sign-in", req.url));
}
if (!isSubscription && req.nextUrl.pathname !== "/account" && req.nextUrl.pathname !== "/sign-in") {
return MiddlewareResponse.redirect(new URL("/account", req.url));
}
return res;
}
export const config = {
matcher: ["/", "/sign-in", "/videos", "/account"],
};
Do I need to set up other files, etc.?
I’d greatly appreciate guidance on resolving this issue. Thank you.