Can't pass header with token for redirect NodeJS

你离开我真会死。 提交于 2021-02-11 15:49:58

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/59241078/cant-pass-header-with-token-for-redirect-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!