无法将带有令牌的标头传递给重定向NodeJS [英] Can't pass header with token for redirect NodeJS

查看:51
本文介绍了无法将带有令牌的标头传递给重定向NodeJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在NodeJS中的登录系统遇到了很大的问题.当我登录时,我创建了登录站点.当我检查登录名和密码是否正确时,我制作了jwt令牌.然后,我想将其传递到标题中,并通过get方法重定向到我的用户页面.我搜索了很多网站,但无法解决此问题.

这是我尝试做的事情:

  const token = jwt.sign({_ id:id},process.env.TOKEN);res.header('auth-token',令牌);res.redirect('/admin/admin_panel'); 

我想像邮递员一样从事这项工作.我将内容类型和身份验证令牌设置为标头,然后重定向到我的路由以对令牌进行身份验证以获取网站.

解决方案

重定向时,您是在告诉客户端向重定向的URL发出请求,而该URL并不是设置标头的人.

如果您的客户端未遵循重定向,则会看到为初始请求正确设置了标题,以及带有/admin/admin_panel Location 标题>

如果要在新请求中提供 token ,可以执行以下操作之一:

  • 使用cookie
  • 使用查询字符串在URL中传递令牌:/admin/admin_panel?token = {token}
  • 其他类型的会话

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.

解决方案

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

这篇关于无法将带有令牌的标头传递给重定向NodeJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆