I am using zeit next for make a isomorphic app with react and an express with express-session (in other server) for make an API.
This component should request private data from the API (request.get('http://0.0.0.0:3100/private')) and show a private page if the user is logged in or redirect to login if not.
In client side this works but when I refresh the private page the I am redirected to login, because the session cookie of client and server side are different. ¿How can I solve this problem?
const Private = (props) => {
return (
<main>
<h1>Private </h1>
<Link href="/">
<a>Home</a>
</Link>
</main>
);
}
Private.getInitialProps = async ({ res }) => {
// getInitialProps is ejecuted in server side when you refresh the page (first time)
// but if you navigate in client side it is ejecuted in client. Server side and client
// have a different session for API calls. Is posible to share the session by client
// and server side?
const response = await request.get('http://someDomain/private');
let body;
if (response.status !== 401) {
body = await response.json();
} else {
if (res) {
res.writeHead(302, { Location: '/login' })
res.end()
} else {
Router.push('/login')
}
}
return {}
}
export default Private;
@PacoRampus as @neerajdngl said While refreshing the page , your cookie is passed to the express server not in browser. so, you are redirected to login page.
You can do something like this:
and your server.js file should ve code like this:
Hope this works for you.