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);