2021年4月10日星期六

Why axios request send with cookie in client, but don't send cookie in serversideprops nextjs?

Good afternoon, I have implemented authorization on MERN stack.

Example: A user logs into the system, a request goes to the backend node js and there the server generates 2 tokens - the first is an access token (first is sent back in the response and then on the client it is written to the redux state and every time it disappears there is a request to the server and access token generate by the refresh token), and the refresh token is sent in cookies. In Axios, I indicated with creditionals and now if I send a request from a client, it is sent immediately with cookies and I can check authorization on the server. But when I send the same request through the serverSideProps, cookies are not attached to the nextjs and the server does not see the authorization.

Why is everything good with the client, but everything is bad with the server side of the nextjs?

Axios config:

export default axios.create({      baseURL: 'http://localhost:5000/api',      withCredentials: true,      headers: {          'Content-Type': 'application/json',      },  })    

ServerSideProps code:

export const getServerSideProps = async () => {        const { data } = await axios.get(          'http://localhost:5000/api/orders/606d2609ddfc99f4548dd2a7/admin'      )        console.log(data) //response: 'Token not found'        return {          props: {},      }  }  
https://stackoverflow.com/questions/67036770/why-axios-request-send-with-cookie-in-client-but-dont-send-cookie-in-serversid April 11, 2021 at 12:34AM

没有评论:

发表评论