Skip to content

Commit

Permalink
Update middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
kaje94 committed Nov 25, 2023
1 parent c8eb32c commit c07c57b
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] || "";
Expand All @@ -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);
Expand Down

0 comments on commit c07c57b

Please sign in to comment.