Express.js JWT Axios : Request Set-Cookie not sending after redirection

Viewed 360

I'm using httponly cookie for JWT joken for API authentication, Initial login I am able to receive Set-Cookie Token after redirection request header doesn't have cookie in that request. Hence I am unable to validate the token.


After login validation, I am sending httponly cookie with JWT token and some data(for local storage) to client browser.

 res.cookie('jwt', token, { httpOnly: true, secure: false });
 res.status(200).send(data); 


After localStorage.setItem("","") in client side, I am redirecting to different page using window.location.href = "/dashboard.html" onload of the page trying to call axios request to express server. but cookies has not sending as part of request header.
The Configuration and troubleshooting followed:

  • withCredentials in ClientSide default axios configuration.

    axios.defaults.withCredentials = true

  • express ServerSide Configuration.

    app.use(function(req, res, next) { res.header('Access-Control-Allow-Origin', "http://localhost:8000"); res.header('Access-Control-Allow-Credentials', true); res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); next(); });

  • cros origin

    cors({credentials: true,""});

  • cookie parser

    app.use(cookieParser());

Checked the res.redirection(302,"http://localhost:8000/dashboard.html") not working. Please check the image for more reference.

Inital Login Request After Redirect Request Header not having cokkie


Does anyone know how to get around this?

0 Answers
Related