From acfed9398e05b1d2c55b336344d9cdcd6a1ce761 Mon Sep 17 00:00:00 2001 From: Eli <13420359+eliasm307@users.noreply.github.com> Date: Thu, 15 Feb 2024 01:39:33 +0000 Subject: [PATCH] middleware - dont add target path in auth page url if target is home page --- src/middleware.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/middleware.ts b/src/middleware.ts index fca6ef6..da27803 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -30,14 +30,16 @@ export async function middleware(req: NextRequest) { console.log("middleware authenticated", "redirecting to", targetUrl.toString()); // todo investigate this doesn't change the url in the browser - return NextResponse.redirect(targetUrl, { url: targetUrl.toString(), status: 302 }); + return NextResponse.redirect(targetUrl, { url: targetUrl.toString() }); } - // todo allow this to redirect to the initial page the user was trying to access // if user is not signed in and the current path is not / redirect the user to / if (!session?.user && req.nextUrl.pathname !== "/auth") { const targetUrl = new URL("/auth", req.url); - targetUrl.searchParams.set(REDIRECT_AFTER_AUTH_QUERY_PARAM_NAME, req.nextUrl.pathname); + if (req.nextUrl.pathname !== "/") { + // if the user is trying to access a page other than the home page save the target so we can redirect them back after they sign in + targetUrl.searchParams.set(REDIRECT_AFTER_AUTH_QUERY_PARAM_NAME, req.nextUrl.pathname); + } console.log("middleware not authenticated", "redirecting to:", targetUrl.toString()); return NextResponse.redirect(targetUrl); }