Closed raphaelbadia closed 2 years ago
You must return redirect
property for that:
https://nextjs.org/docs/api-reference/data-fetching/get-server-side-props#redirect
This would be solved in the upcoming version https://github.com/hoangvvo/next-connect/pull/196
import { createRouter } from "next-conect";
const router = createRouter();
router.get(() => {
return {
redirect: {
destination: "/",
permanent: false,
},
};
});
export async function getServerSideProps({ req, res }) {
return router.run(req, res);
}
Very interesting.
Currently my workaround was really ugly : in my middlewares, I use the function next
like so :
return next({
redirect: { destination: APP_ROUTES.AUTH.LOGIN, permanent: false },
});
this throws an error, so I can catch it in my getServerSideProps and return the error object 😔
I will follow the upcoming version with great interest, thanks for the update !
Hello,
the documentation indicates that we can't redirect using res.redirect() in a middleware.
I am making an authentication middleware that should redirect to the login page when the token is expired and stop the middleware chain if the user is not authenticated. But I can't seem to find a solution to handle that from the middlewares. How should I do this ?
Thanks in advance !