I am using kubernetes for backend(microservices) and react(client).
I am able to send / authenticate via cookies when the request is being sent from the browser(client side) but when the app first loads(server side), I am unable to authenticate.
Which means it is not sending requests from the server side.
Proof:
AppComponent.getInitialProps = async (appContext) => {
let data = {};
const client = apiRequest(appContext.ctx); // <== custom axios instance
console.log(`${appContext.ctx.req.headers}`); // <== This is showing null
try {
const res = await client.post('/api/users/currentuser'); // <== to check logged in user
data = res.data;
catch (err) {
if (err) data = { currentUser: null }
}
let pageProps = {};
if (appContext.Component.getInitialProps) {
pageProps = await appContext.Component.getInitialProps()
}
return { pageProps, ...data }
}
Custom axios function:
export const apiRequest = ({ req }) => {
if (typeof window === undefined) {
// we are on server side
return axios.create({
baseURL: 'http://ingress-nginx-controller.ingress-nginx.svc.cluster.local',
// to pass cookies and hostname needed by ingress while resolving the request coming
headers: req.headers
})
}
return axios.create({
baseURL: "/"
})
}