Object is undefined, but when I console.log them always "works" Node.js express

302 Views Asked by At

When I'm trying to destructure object in my express middleware:

const checkIfLoggedIn = (req, res, next) => {
    try {
        const { token } = req.cookies['jwt-auth'];
        console.log('token: ', token);
        req.token = jwt.verify(token, SECRET);
        next();

    } catch (err) {
        console.error(err);
        res
            .status(401)
            .json({
                ok: false,
                message: 'ERROR: you need to be logged in'
            });
    }
}

My app prints this:

TypeError: Cannot destructure property 'token' of 'req.cookies.jwt-auth' as it is undefined.
    at checkIfLoggedIn (/home/bader/Projects/be_productive/services/authorization.middleware.js:8:17)
    at Layer.handle [as handle_request] (/home/bader/Projects/be_productive/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/bader/Projects/be_productive/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/index.js:328:13)
    at /home/bader/Projects/be_productive/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/index.js:286:9
    at Function.process_params (/home/bader/Projects/be_productive/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/index.js:346:12)
    at next (/home/bader/Projects/be_productive/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/index.js:280:10)
    at cookieParser (/home/bader/Projects/be_productive/node_modules/.pnpm/[email protected]/node_modules/cookie-parser/index.js:57:14)
    at Layer.handle [as handle_request] (/home/bader/Projects/be_productive/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/bader/Projects/be_productive/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/index.js:328:13)
    at /home/bader/Projects/be_productive/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/index.js:286:9
token:  (Token from front-end (I change this line))

I don't know why It prints error, especially that console.log works and print correct token

I try to use dot instead of destructuring in every combination and it still doesn't work

1

There are 1 best solutions below

3
Live bug help - www.dialect.so On

You should add an explicit check before destructuring the jwt-auth token in case it isn't present:

const checkIfLoggedIn = (req, res, next) => {
    try {
        const jwtAuth = req.cookies['jwt-auth']
        if (jwtAuth === undefined) {
          throw new Error('missing token');
        }
        const { token } = jwtAuth
        console.log('token: ', token);
        req.token = jwt.verify(token, SECRET);
        next();

    } catch (err) {
        console.error(err);
        res
            .status(401)
            .json({
                ok: false,
                message: 'ERROR: you need to be logged in'
            });
    }
}