0

I have this issue of setting up the authentication of my MERN stack app. I red this question but no answer in there too. I can see the cookie set from the back-end server in the postman cookies but the same not working in the chrome or any other web browsers. Use JWT token and set that token into the HttpOnly cookie like this in node back-end. Using this cookie npm package to create the HttpOnly cookie from the back-end node server.

res.setHeader('Set-Cookie', serialize("jwt", token, {
          httpOnly: true,
          secure: true,
          sameSite: "none",
          maxAge: 60 * 60,
          path: "/"
}));

when I add credentials true option to the axios client, it gives error and all the routes are not working.

const Axios = axios.create({
    baseURL:  baseUrl,
    withCredentials: true
})

cors policy configured in the back-end as below

app.use(cors({origin: '*'}));

The flow is like this:

  1. User submit the nextjs front-end form with username and password
  2. Axios use to send the data to the nodejs server
  3. Generate the token sign and set it to the HttpOnly cookie and token send with the json response to the front-end.
asela daskon
  • 496
  • 1
  • 8
  • 21

0 Answers0