From c07c57b19ac9ca1a65e23a6ce6bf0567099bb3e7 Mon Sep 17 00:00:00 2001 From: kaje94 Date: Sat, 25 Nov 2023 11:49:20 +0530 Subject: [PATCH] Update middleware --- src/middleware.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/middleware.ts b/src/middleware.ts index 3cc8d44..964064d 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -5,13 +5,6 @@ import { COUNTRIES } from "./utils/countries"; export async function middleware(request: NextRequest) { const pathname = request.nextUrl.pathname; - if (new RegExp("/(dashboard.*)").test(request.nextUrl.pathname)) { - const session = await getSession(); - if (!session) { - return NextResponse.rewrite(new URL(`/api/auth/login?returnTo=${request.nextUrl.pathname}`, request.url)); - } - } - // country code should be available only after deployed const userCountryCode = request.geo?.country || "LK"; const pathLocale: string = pathname.split("/").filter((item) => item !== "")[0] || ""; @@ -21,6 +14,13 @@ export async function middleware(request: NextRequest) { return NextResponse.redirect(new URL(`/${userCountryCode}/${pathname}`, request.url)); } + if (new RegExp(`/${userCountryCode}/(dashboard.*)`).test(request.nextUrl.pathname)) { + const session = await getSession(); + if (!session) { + return NextResponse.redirect(new URL(`/api/auth/login?returnTo=${request.nextUrl.pathname}`, request.url)); + } + } + const res = NextResponse.next(); res.headers.set("x-pathname", request.nextUrl.pathname); res.headers.set("x-locale", pathLocale);