5 months ago
I want to send a request to my backend and get a session, but in railway an infinite middleware loop is running and no request is sent to the backend.
4 Replies
5 months ago
Hey there! We've found the following might help you get unblocked faster:
If you find the answer from one of these, please let us know by solving the thread!
5 months ago
751a3e57-d06b-4c1e-ac50-f29b409794da
5 months ago
Add a custom header like x-no-middleware: true to your backend request and have the middleware skip any requests with that header.
if (req.headers.get('x-no-middleware') === 'true') {
return NextResponse.next();
}5 months ago
look
export const middleware = async (request: NextRequest) => {
const { data: session } = await betterFetch<typeof auth.$Infer.Session>(
${process.env.NEXT_PUBLIC_SERVER_URL}/auth/get-session,
{
headers: await headers(),
credentials: "include",
},
);
// If the user isn't signed in and the route is private, redirect to sign-in
if (!session && !isPublic(request)) {
return NextResponse.redirect(new URL("/auth/sign-in", request.url));
}
// Catch users who do not have 'onboardingComplete: true'
// Redirect them to the /onborading route to complete onboarding
if (session && !session.user.onboardingComplete && !isOnboarding(request)) {
return NextResponse.redirect(new URL("/onboarding", request.url));
}
// Catch users who do have logged and have 'onboardingCompelete: true'
// Redirect them to the /dashboard route to continue
if (session && ((isOnboarding(request) && session.user.onboardingComplete) || isAuth(request))) {
return NextResponse.redirect(new URL('/dashboard', request.url));
}
return NextResponse.next();
};
export const config = {
matcher: ["/((?!api|_next/static|_next/image|.*\\.png$).*)"],
};
When I use fetch, the session doesn’t work
it’s not sending any request to the backend. I’m sure the env is fine though. So why is it looping and calling itself? Not sure what’s going on, but it was working fine on my computer.