9

As you can see in the NodeJS code I'm grabbing the user from the token from Postman provided in the URL but I'm struggling to get back the _id property from the given token number. What I want is that id in the decoded._id so that I can use that id in further operations

const jwt = require('jsonwebtoken')
const User = require('../model/users')
const auth = async (req, res, next) => {
    try {
          const token = req.header('Authorization').replace('Bearer', '')
          const decoded = jwt.verify(token, 'thisisfromabhishek')

`the property _id does not exist on decoded`
          const user = await User.findOne({ _id: decoded._id, 'tokens.token': token })

        if (!user) {
            throw new Error()
        }
        req.token = token
        req.user = user
        next()
       } catch (e) {
        res.status(401).send({ error: 'Please authenticate.' })
    }
}

module.exports = auth
jps
  • 20,041
  • 15
  • 75
  • 79
Abhishek Vats
  • 91
  • 1
  • 4
  • could you log decode variable ? – mohammad Naimi Oct 07 '21 at 10:46
  • it not showing me any error in console its just not grabbing the _id property some autocomplete from the editor but because of that I'm not able to grab _id – Abhishek Vats Oct 07 '21 at 13:18
  • can you please show your token? – jps Oct 07 '21 at 13:40
  • "tokens" : [ { "token" : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MTVlZWY1MmM4NTZlYmFkMTM3NTIzNjIiLCJpYXQiOjE2MzM2MTE2MDJ9.13oo0gCXb_fGyElG4g3Uakf2g7LGdih6fV5pHOK6vkI", "_id" : ObjectId("615eef52c856ebad13752364") } ] – Abhishek Vats Oct 07 '21 at 14:00
  • so your token is stored in a JSON. What is the exact value of `token` that you pass to `jwt.verify(token...`? It should be only the token itself, `eyJhb.....` – jps Oct 07 '21 at 14:31
  • this value of token passing in "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MTVlZWY1MmM4NTZlYmFkMTM3NTIzNjIiLCJpYXQiOjE2MzM2MTE2MDJ9.13oo0gCXb_fGyElG4g3Uakf2g7LGdih6fV5pHOK6vkI" in Postman headers in Authorization key – Abhishek Vats Oct 07 '21 at 14:52
  • if you have a string "Bearer eyJhbGciOiJIUz..." and then just remove the word "Bearer", you still have a blank in front of the token, e.g. " eyJhbGciOiJIUz..." . Could this be the problem? – jps Oct 07 '21 at 15:26
  • I did tried that after your recommendation but still I'm not getting the desired result I think its of the _id only because it's still showing me the same and it's getting the _id – Abhishek Vats Oct 08 '21 at 13:16

3 Answers3

26

Typescript don't know about _id in token. Data declaration should help.

interface JwtPayload {
  _id: string
}

const { _id } = jwt.verify(token, 'thisisfromabhishek') as JwtPayload

req.user = await User.findOne({ _id, 'tokens.token': token })

jps
  • 20,041
  • 15
  • 75
  • 79
roman retiunsky
  • 261
  • 2
  • 4
0

you have not given space after Bearer.

const token = req.header('Authorization').replace('Bearer', '') //here is the problem place space after Bearer
          const decoded = jwt.verify(token, 'thisisfromabhishek')
Asim Imam
  • 313
  • 1
  • 3
  • 12
0

Typescript don't know about _id in token, add JwtPayloqd type or interface with id

interface JwtPayload {
    id: string;
}

const decoded = jwt.verify(token, process.env.JWT_SECRET!) as JwtPayload;
console.log(decoded);
  • 1
    Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Sep 18 '22 at 17:53