i try to send token use header, (login form to index page use the 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'); });
my index file is
app.get('/index',verift, function(req, res, next) { res.render('index'); });
the page redirect index page ,but the token not send the index page
Advertisement
Answer
If I understood you correctly, you expect that the authtok
header will be set by the browser when a request to /index
page is made.
Headers are not cookies (tho cookies are passed in a header). If you set a header in a response, the browser will receive that header but it will NOT be inserted in any of the subsequent queries made by the browser.
In this case you have to explicitly read, store and set authtok
header in a subsequent request.