I am playing around with user authentication right now with Nextjs. I am curious the best way to go about redirecting a user from a page if they are not signed in.
Right now my implementation is this: “` const User: NextPage<MyInterface> = ({ redirect }) => { useEffect(() => { if (redirect) router.push(“/login”); }, [redirect]);
return <div className=””>You shouldn’t see this</div>; };
export const getServerSideProps: GetServerSideProps<MyInterface> = async (ctx) => {
const { token } = ctx.req.cookies;
if (!token) {
return { props: { redirect: true } };
} } “`
Let me know your thoughts! Thanks!
submitted by /u/Zealousideal_Water_6
[link] [comments]