节点js通过header发送token
node js send token through the header
我尝试使用 header 发送令牌,(使用 header 索引页面的登录表单)
router.post('/login',async(req,res)=>{
const {error} = logvali(req.body);
if(error) return res.status(400).send(error.details[0].message);
//check if the email exist
const user = await User.findOne({email: req.body.email});
if(!user) return res.status(400).send('Email is Wrong');
//password is correct
const vapass = await bcrypt.compare(req.body.password , user.password);
if(!vapass) return res.status(400).send('Password is Wrong');
const token =jwt.sign({_id: user._id},process.env.TOK);
res.header('authtok',token).redirect('/index');
});
我的索引文件是
app.get('/index',verift, function(req, res, next) {
res.render('index');
});
页面重定向索引页,但令牌不发送索引页
如果我没理解错的话,您希望 authtok
header 会在请求 /index
页面时由浏览器设置。
Headers 不是 cookie(cookie 在 header 中传递)。如果您在响应中设置 header,浏览器将收到 header,但它不会被插入到浏览器发出的任何后续查询中。
在这种情况下,您必须在后续请求中显式读取、存储和设置 authtok
header。
我尝试使用 header 发送令牌,(使用 header 索引页面的登录表单)
router.post('/login',async(req,res)=>{
const {error} = logvali(req.body);
if(error) return res.status(400).send(error.details[0].message);
//check if the email exist
const user = await User.findOne({email: req.body.email});
if(!user) return res.status(400).send('Email is Wrong');
//password is correct
const vapass = await bcrypt.compare(req.body.password , user.password);
if(!vapass) return res.status(400).send('Password is Wrong');
const token =jwt.sign({_id: user._id},process.env.TOK);
res.header('authtok',token).redirect('/index');
});
我的索引文件是
app.get('/index',verift, function(req, res, next) {
res.render('index');
});
页面重定向索引页,但令牌不发送索引页
如果我没理解错的话,您希望 authtok
header 会在请求 /index
页面时由浏览器设置。
Headers 不是 cookie(cookie 在 header 中传递)。如果您在响应中设置 header,浏览器将收到 header,但它不会被插入到浏览器发出的任何后续查询中。
在这种情况下,您必须在后续请求中显式读取、存储和设置 authtok
header。