Describe the bug
router.replace not replacing /api/auth/signin just pushing it, I think this is indeed a bug with next-auth since replacing pathname with /home (custom page) results in expected behaviour, using Router also didn't solve the problem
To Reproduce
This is simplified code:
export const MainPage: React.FC<MainPageProps> = ({}) => {
const [session, loading] = useSession();
const router = useRouter();
useEffect(() => {
console.log(session);
if (!session && !loading) {
router.replace({
pathname: "/api/auth/signin",
});
}
}, [loading]);
if(session) {
return (
<>
<NavBar/>
<Page/>
</>
);
} else if (loading) {
return <>
<h1>Loading....</h1>
</>;
} else {
return null;
}
}
Expected behaviour
signin page should be replaced with MainPage, not pushed
Hi there!
This is not a bug, as the API route is not a normal Next.js page - API routes in Next.js can't be linked to in this way.
The built-in pages actually use Preact and are 100% server side rendered, so that they don't rely on client side JavaScript. They use Preact specifically, to keep down the amount of code that needs to be loaded server side in the API route.
If you use a custom sign in page (e.g. pages/signin.js) you can link to your sign in page like this though.
The v3 docs have some examples of how to use custom sign in pages with the current beta - you can also use them in v2 but there are some changes in v3 so you might want to try it out with the beta now to avoid having to refactor it later.
Closing as is not a bug (and we can't do anything about it) but appreciate it's an odd thing to run into, so hope this helps someone else in future if they run into the same issue!