-
Hi, I need to check the existence of a JWT token to navigate the user to the correct route. I put my token validation stuff in async (context: NextPageContext): Promise<{ props: { token: string } }> => {
const { token } = nextCookies(context);
console.log(context) // {}
if (!token) {
if (typeof window === 'undefined') {
// ❌ I get no 'res' key on context for redirection so this throws an error
context.res.writeHead(302, { Location: '/phone-get' });
context.res.end();
} else {
/* ❌
this wouldn't work as well. Because the router is not available here.
The next router only works on the client side
*/
Router.replace('/phone-get');
}
}
return {
props: { token },
};
}; |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 23 replies
-
getStaticProps is not a middleware before rendering the page. Is executed on the build step. So I think that is not possible. |
Beta Was this translation helpful? Give feedback.
-
An extension of this request, is it possible to redirect the user before the page is generated in getStaticProps? I would like interpret the data fetched in getStaticProps and then stop the generation of that page if necessary. |
Beta Was this translation helpful? Give feedback.
-
You should redirect in useEffect |
Beta Was this translation helpful? Give feedback.
-
I would also like to have some way to do the redirect on the server when accessing a static page with authentication cookie. I tried to do client-side redirect in I also tried to do the redirect in a separate JS script tag in HTML head that would run before the page could be painted, like this:
unfortunately still, no luck, there was a flash between static page and authenticated page. I ended up rendering the whole page in a div with |
Beta Was this translation helpful? Give feedback.
-
Opened a RFC here: #14890 |
Beta Was this translation helpful? Give feedback.
-
Using What I did is just something like it: import type { GetStaticProps } from 'next';
const Join = () => <div></div>;
export const getStaticProps: GetStaticProps = async () => ({
redirect: {
destination: 'https://app.woovi.com/join',
permanent: true,
},
});
export default Join; |
Beta Was this translation helpful? Give feedback.
getStaticProps is not a middleware before rendering the page. Is executed on the build step. So I think that is not possible.