Darek N
Darek N

Reputation: 51

Can't pass header with token for redirect NodeJS

I've got big problem with my login system in NodeJS. I created login site, when i'm logging in. When i check if login and password is correct i make jwt token. Then i would like to pass it into header and redirect to my user page by get method. I searched a lot of sites and I' cant solve this problem.

This is what I try to do:

const token = jwt.sign({_id: id}, process.env.TOKEN);


 res.header('auth-token', token);
 res.redirect('/admin/admin_panel');

I would like to this work like Postman. I set content type and my auth token to header and redirect to my route for authenticate token to get site.

Upvotes: 1

Views: 1004

Answers (1)

Marcos Casagrande
Marcos Casagrande

Reputation: 40374

When you redirect, you're telling the client to issue a request to the redirected URL, and that URL is no the one setting the header.

If your client does not follow redirects, you will see the header set correctly for the initial request, as well as a Location header with /admin/admin_panel

You can do one of the following if you want token available in the new request:

  • Use cookies
  • Pass the token in the URL using querystring: /admin/admin_panel?token={token}
  • Other type of session

Upvotes: 2

Related Questions