Error: NEXT_REDIRECT
Answered
Giant Angora posted this in #help-forum
Giant AngoraOP
Hello there,
I am having the following error while trying to use the
Here is my component:
I am having the following error while trying to use the
redirectfunction from next/navigationServer Error
Error: NEXT_REDIRECT
This error happened while generating the page. Any console logs will be displayed in the terminal window.Here is my component:
import { type GetServerSidePropsContext } from "next";
import { getServerAuthSession } from "@/server/auth";
import { useSession } from "next-auth/react";
import { redirect } from "next/navigation";
export default function Dashboard() {
const { data: session } = useSession();
if (!session || !session.user) {
redirect("auth/signin");
}
return (
<>
<h1>Protected Page</h1>
<p>You can view this page because you are signed in.</p>
</>
);
}
export async function getServerSideProps(context: GetServerSidePropsContext) {
return {
props: {
session: await getServerAuthSession(context),
},
};
}Answered by @ts-ignore
I think you are using
pages dir, if that's the case then redirect from next/navigation can't be used21 Replies
@Giant Angora Hello there,
I am having the following error while trying to use the `redirect`function from `next/navigation`
Server Error
Error: NEXT_REDIRECT
This error happened while generating the page. Any console logs will be displayed in the terminal window.
Here is my component:
import { type GetServerSidePropsContext } from "next";
import { getServerAuthSession } from "@/server/auth";
import { useSession } from "next-auth/react";
import { redirect } from "next/navigation";
export default function Dashboard() {
const { data: session } = useSession();
if (!session || !session.user) {
redirect("auth/signin");
}
return (
<>
<h1>Protected Page</h1>
<p>You can view this page because you are signed in.</p>
</>
);
}
export async function getServerSideProps(context: GetServerSidePropsContext) {
return {
props: {
session: await getServerAuthSession(context),
},
};
}
I think you are using
pages dir, if that's the case then redirect from next/navigation can't be usedAnswer
Giant AngoraOP
Oh okay, so what's the workaround here?
Giant AngoraOP
Kinda getting used to Next
sorry this is for server side, for client side you've to use
useRouter().replaceGiant AngoraOP
Should I use
useRouter?Giant AngoraOP
It will work with my Server Side component?
make sure the import is from
next/router@Giant Angora It will work with my Server Side component?
I am not sure, you should give it a shot first
its been quite long since I used pages dir
export async function getServerSideProps(context: GetServerSidePropsContext) {
const session = await getServerAuthSession(context)
if (!session || !session.user) {
return {
redirect: '/auth/signin',
props: {}
}
}
return {
props: {
session,
},
};
}Giant AngoraOP
Oh wow, perfect
Let me try this one
Added
props: {}yep
Giant AngoraOP
Oh you edited it
i forgot it too lol
Giant AngoraOP
Yeah, it was throwing me an error
Working perfect now, thanks a lot!