Search code examples
node.jsauthenticationverification

TypeError: Cannot read properties of undefined (reading 'authorization')


TypeError: Cannot read properties of undefined (reading 'authorization') at verifyToken (file:///C:/Users/awan7/Downloads/JWT-AUTH/backend/midleware/VerifiedToken.js:4:35)

import jwt from "jsonwebtoken";

export const verifyToken = (res, req, next) => {
    const authHeader = req.headers['authorization'];
    const token = authHeader && authHeader.split(' ')[1];
    if (token == null) return res.sendStatus(401);
    jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, (err, decoded) => {
        if (err) return res.sendStatus(403);
        req.email = decoded.email;
        next();
    })
}

i did't know what is..


Solution

  • import jwt from "jsonwebtoken";
    
    export const verifyToken = (res, req, next) => { --->> i change my code from this
    
    export const verifyToken = (req, res, next) => { <<---  in to this
    
        const authHeader = req.headers['authorization'];
        const token = authHeader && authHeader.split(' ')[1];
        if (token == null) return res.sendStatus(401);
        jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, (err, decoded) => {
            if (err) return res.sendStatus(403);
            req.email = decoded.email;
            next();
        })
    }