【发布时间】:2022-01-12 08:37:08
【问题描述】:
我正在尝试访问与 cookie 一起存储的令牌值。
[Symbol(kHeaders)]: {
host: 'localhost:3000',
connection: 'keep-alive',
'content-length': '20',
'cache-control': 'max-age=0',
'upgrade-insecure-requests': '1',
origin: 'http://localhost:3000',
'content-type': 'application/x-www-form-urlencoded',
'user-agent': 'Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'navigate',
'sec-fetch-user': '?1',
'sec-fetch-dest': 'document',
referer: 'http://localhost:3000/',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
cookie: 'undefined=undefined; token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MWFjNTZkYjMzN2UzMzQxM2M4YjNjNDYiLCJpYXQiOjE2Mzg4NjUwNzUsImV4cCI6MTYzODk1MTQ3NX0.K03ISYNiTjZitr74H1-cN1CRTsEbJbO6sVh-XqAF1bA'
},
我试过res.cookie,但它返回的都是未定义的。我再次尝试获取“主机”的值,但也返回未定义。在某些情况下,我试图将 JWT 存储在 cookie 中,并通过 auth 中间件访问相同的令牌。
router.post('/login', async(req,res)=>{
const {username, email, password} = req.body;
try{
const user = await User.findByCredentials(username, password);
const token = await user.generateAuthToken();
res.cookie('token', token);
console.log('generated token: ',token);
res.redirect('/');
}catch(e)
{
console.log('it did not work: ', e);
}
});
在这里我试图访问该值,但我遇到了困难
const jwt = require('jsonwebtoken');
const User = require('../models/user');
const auth = async(req, res, next) =>{
console.log('response data from auth.js: ',req)
}
module.exports = auth;
【问题讨论】: