0

I was trying to get jwtauthtoken from user and passing it to a getuserId() which was imported in that js file. But i was getting undefined value instead of decoded id that was returned by the function.

In the getUserId() it displays the decoded token

My console output:

user id 5f68efb234a7656

On get request : undefined

Anyone can help to me resolve the problem.

personalDetailsController.js

module.exports.personaldetail_get = async (req,res) => {

    const token = req.cookies.jwtauthtoken;

        let userId = await getUserId(token);
        console.log("On get request : "+ userId); // output On get request : undefined

    res.render('Candidate/personal', { csrfToken: req.csrfToken() });

}

getUserId.js

module.exports.getUserId =  (tokenid) => {

    const token = tokenid;

    try{

        if (token) {

            jwt.verify(token,'2308199919990823', async (err, decodedToken) => {
                if(err){
                    console.log(err);
                    return null;
                } else {
                    console.log("user id " + decodedToken.id); // Output user id 5f68efb234a7656
                    return decodedToken.id;
                }

            });
        } else {
            return null;
        }

    }
    catch(err)
    {
        console.log(err)
        return null;
    }

}

2 Answers2

3

const decodedToken = await getUserId(token) means two things :

  • getUserId must return a Promise.
  • decodedToken is given by the resolution of this Promise.
getUserId = tokenid => {
    return new Promise(resolve => {
        jwt.verify(tokenid, '2308199919990823', (err, decodedToken) => resolve(decodedToken))
    })
}
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
1

you forgot the add a return on your function call...

Also the try catch block you have will not work since your verify function is asynchronous...you need to wrap this in a Promise...

module.exports.getUserId =  (tokenid) => {

    const token = tokenid;

    if (token) {
        return new Promise((reject, resolve) => {
            jwt.verify(token,'2308199919990823', async (err, decodedToken) => {
                if(err){
                    console.log(err);
                    return null;
                } else {
                    console.log("user id " + decodedToken.id); // Output user id 5f68efb234a7656
                    return resolve(decodedToken.id);
                }

            });
        });
    }
}
bloo
  • 1,416
  • 2
  • 13
  • 19
  • This will only return `jwt.verify` which is an asynchronous function. It will not return the value of the token, which is the expected _result_. Downvoted – Jeremy Thille Sep 18 '20 at 07:44
  • Yeah sorry, i had just realized that this needs to be wrapped in a Promise for it to work properly – bloo Sep 18 '20 at 07:47
  • 1
    Made the edit, please check and verify...it should return a Promise of a token...thanks – bloo Sep 18 '20 at 07:49
  • Fair enough, canceled my downvote. But you should `reject()` instead of `return null`. – Jeremy Thille Sep 18 '20 at 07:52