Next.js Discord

Discord Forum

weird behavior on redirect with application served on a different base path

Unanswered
English Spot posted this in #help-forum
Open in Discord
English SpotOP
Hello, I’m fairly new to using nextjs but so far it’s been a good learning experience, I am however having a bit of a struggle here when I’m trying to redirect a user to a page under the root of my base path. I’m using app router

Application has a base path of “/xyz” and I’m using next-auth for authentication.

My next-auth configuration is a bit complex too, I’m using it with keycloak for SSO so I have custom login / logout pages to handle the session initialization logic and redirects.

My app has a route of “/login” and “/logout” and these can be accessed in the browser by going to “host/xyz/login” and “host/xyz/logout” respectively.
If I use the “redirect” function from “next/navigation” with “/login” I was expecting to be redirected to “host/xyz/login” but unfortunately I’m redirected to the root of the server, host/login.

So I thought, well maybe redirect doesn’t take into consideration the base path I set for next and tried to use “/xyz/login” as the parameter for redirect, but this results in being redirected to “host/xyz/xyz/login”.

What am I doing wrong here? Has anyone faced the same issue? (I’ve searched here and elsewhere and couldn’t find anyone with this problem)

1 Reply

English SpotOP
Absolute paths seem to be the only solution for me at the moment, that or redirects from the root paths to the ones with my base path prepended… but those don’t seem like a solution, more like a hack to fix a problem