Home > Net >  jwt authorization header is missing
jwt authorization header is missing

Time:10-08

After user is logged in I sign him a jwt. then my middleware is trying to validate the token by grabbing the authorization header but there is such header. Im trying to print out the request header by printing it but its undefined Login code:


const login = require("express").Router();
const signUpTemplate = require('../src/Backend/SignupModel')
const bcrypt = require('bcrypt')
const jwt = require('jsonwebtoken');
login.post("/login", async function (request, response) {
    try{
        const {Email,password} = request.body
        console.log({Email,password});
        console.log('before finding');
        signUpTemplate.findOne({Email:Email}).then(async (user) => {
            if(!user){
                console.log('found user');
                console.log('username/password is not exist');
                response.status(400).send({msg:"username/password is not exist"})
            }
            else{
                if(await bcrypt.compare(password, user.password)){
                    const token = jwt.sign(
                        { user_id: user.id, Email },
                        process.env.TOKEN_KEY,
                        {
                          expiresIn: "2h",
                        }
                    );
                    user.token = token;
                    response.status(200).json(user);
                }
                else{
                    console.log('username/password is not exist');
                    response.status(400).send({msg:"username/password is not exist"})
                }
            }
        })
    }
    catch(e){
        console.error(e);
    }
});
module.exports = login;


const verifyJwt = (req,res,next) => {
    console.log('entered middle');
    console.log(req.headers);
    console.log(req.headers['authorization']);
    // const authHeader = req.headers['authorization'];
    // if (!authHeader) return res.sendStatus(401);
    return res.status(200).send('test works');
}

module.exports = verifyJwt

If I try to print in my middleware console.log(req.headers['authorization']) its undefined

CodePudding user response:

Foolish mistake, I didn't send auth header in the api request from client :)

  • Related