Next.js Discord

Discord Forum

is it possible to get path from Next app router with Id like [id] instead of a custom id

Unanswered
California Gull posted this in #help-forum
Open in Discord
California GullOP
I am using nextjs App router and next-intl localize pathname api. I would like to know if its possible to get pathname just like we mention in the navigation file. Currently I get pathname from NextRequest with an id like /de/users/5 but I have it in my navigation file like /de/users/[id].

Maybe this is a completely wrong approach. It will be helpful for me if you guide me how do I solve this?

In middleware I am checking if its a private route and user don't have a token then redirect the user to home page

  // middleware.ts

    const publicRoutes = ["/", "/en", "/de"];
    // process "privatePathnames" and returns ["/en/users/[id]", "/de/users/[id]"]
    const privateRoutes = privatePathMakerWithLocale(); 
    export default async function middleware(req: NextRequest) {
       if (publicRoutes.includes(pathname) && isVerifiedUser) {
          console.log("public route");
          return NextResponse.redirect(`${origin}/${locale}/users`);
       }
       // here the pathname is "/de/benutzer/5" while privateRoutes has ["/en/users/[id]", "/de/users/[id]"]
       if (privateRoutes.includes(pathname) && !isVerifiedUser) { 
          console.log("private route");
          return NextResponse.redirect(`${origin}/${locale}`);
       }
    }
    export const config = {
      // Match only internationalized pathnames
      matcher: ['/', '/(de|en)/:path*']
    };

0 Replies