Next.js Discord

Discord Forum

Middleware logic redirection

Unanswered
Kuchi posted this in #help-forum
Open in Discord
KuchiOP
I'm getting redirected to /unsupported-country when session.location.location.country === "CA"
which is not what's supposed to be happen, its supposed to redirect only if its !== "CA" or !=="US", unsure why...

import { NextResponse } from "next/server";
import { getIronSession } from "iron-session/edge";
import { sessionOptions } from "./lib/session";

export const middleware = async (req) => {
  const res = NextResponse.next();

  const path = req.nextUrl.pathname;
  console.log("..........................");

  //get current session if it exists
  const session = await getIronSession(req, res, sessionOptions);
  //grab current session + display current session data
  const { location } = session;

  if (session) {
    console.log("location in session is : ", session);
  }

  //if there's no session, get user IP and set the browser session for user location
  if (!session.location) {

    //   fetch user IP + local data and set it in a session cookie
    const url =
      process.env.NODE_ENV === "production"
        ? process.env.PROD_URL
        : process.env.DEV_URL + `api/geolocation-api`;

    const res_location = await fetch(url);
    const locationData = await res_location.json();
    session.location = locationData;
    await session.save();

    //If session is created already - redirect users outside of CA/US to Unsupported page
   
    if (
      session.location.location.country &&
      location?.location.country !== "CA" &&
      location?.location.country !== "US" &&
      path !== "/unsupported-country"
    ) {
     
      return NextResponse.redirect(new URL("/unsupported-country", req.url));
    }
  }
  
  // REDIRECT LOGIC HERE //

  //if user lands on any page but they're from alberta, send them to the Alberta page version of the req.url
  const url = req.nextUrl.clone();
  if (
    session.location.location &&
    location?.location.country === "CA" &&
    location?.location.region === "Alberta" &&
    url.pathname.indexOf("/AB") === -1
  ) {
    const albertaURL = url.origin + "/" + "AB" + url.pathname;
    return NextResponse.redirect(new URL(albertaURL, req.url));
  }

  return res;
};

export const config = { matcher: "/((?!.*\\.).*)" };

5 Replies

If there is a location, you can try like this.

if (session.location.location.country) {
if(location?.location.country !== "CA" ||
location?.location.country !== "US"){
return NextResponse.redirect(
new URL("/unsupported-country", req.url));
} else {
return NextResponse.next();
}
}
If the session has that information as you want, it continues without doing anything.
location?.location.country !== "CA" || location?.location.country !== "US"
If this information is not available, redirect.
------------------------------------------------------------
These two lines of code seem so ridiculous to me.
path !== "/unsupported-country"
return NextResponse.redirect(new URL("/unsupported-country", req.url));
location?.location.country !== "CA" || location?.location.country !== "US"
If one of the two matches here, it continues, if one of the two does not match, this code block is activated.

return NextResponse.redirect(new URL("/unsupported-country", req.url));