Skip to content

Commit

Permalink
fix(webapp): 🐛 update middleware logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kaje94 committed Jul 13, 2024
1 parent d9534ff commit ce70e33
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions apps/webapp/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,27 @@ export async function middleware(request: NextRequest) {

// country code should be available only after deployed
const userCountryCode = request.geo?.country?.toLowerCase() || "lk";
const pathLocale: string =
pathname
.split("/")
.filter((item) => item !== "")[0]
?.toLowerCase() || "";
const matchingLocal = COUNTRIES[pathLocale];
const pathLocale: string = pathname.split("/").filter((item) => item !== "")[0] || "";
const pathLocaleLowerCase: string = pathLocale?.toLowerCase();
const matchingLocal = COUNTRIES[pathLocaleLowerCase];

// Redirect to the correct route by if a valid route does not exist
if (!matchingLocal) {
if (isCrawler && pathLocale !== BOT_LOCALE) {
if (isCrawler && pathLocaleLowerCase !== BOT_LOCALE) {
return NextResponse.redirect(new URL(`/${BOT_LOCALE}/${pathname}`, request.url));
} else if (!isCrawler) {
if (pathLocale === BOT_LOCALE) {
if (pathLocaleLowerCase === BOT_LOCALE) {
return NextResponse.redirect(new URL(pathname?.replace(`/${BOT_LOCALE}`, `/${userCountryCode}`), request.url));
} else {
return NextResponse.redirect(new URL(`/${userCountryCode}/${pathname}`, request.url));
}
}
}

if (matchingLocal && pathLocale !== pathLocaleLowerCase) {
return NextResponse.redirect(new URL(`/${userCountryCode}/${pathname}`, request.url));
}

// Check if a valid user is trying to access the dashboard route
if (new RegExp(`/${userCountryCode}/(dashboard.*)`).test(request.nextUrl.pathname)) {
const session = await getSession();
Expand Down

0 comments on commit ce70e33

Please sign in to comment.