0

I am quite new to Node JS, I have some problem to retrieve the accessToken from the generateAccessToken method when it called from the login exports function. After I compile the code, it shows an undefined value in the console log.

I have attempted to add async on the generateAccessToken and the export function but both of them are not working. I have validated the accessToken can be printed within the generateAccessToken function.

exports.generateAccessToken = function(req,res) {
    const user = {
        username: req.body.username
    };
    const accessToken = jwt.sign(user, process.env.ACCESS_TOKEN_SECRET, { expiresIn: '15s' });
    User.update({ access_token: accessToken }, { where: { username: user.username } }).then((resp) => {
        //console.log(resp);
        if (resp != 1) {
            res.status(403).send({ error: "Failed to update access token" });
        }
        return accessToken;
    }).catch((err) => {
        res.status(500).send({ error: err.message });
    });
}

exports.login = function (req, res) {
    const username = req.body.username;
    const accessToken = module.exports.generateAccessToken(req,res);
    console.log(accessToken); //return undefined value here.
    const refreshToken = generateRefreshToken(username);
    return res.status(200).send({ message: "You have successfully login!", refreshToken: refreshToken, accessToken: accessToken });
}

Update:

exports.generateAccessToken = function(req,res) {
    const user = {
        username: req.body.username
    };
    const accessToken = jwt.sign(user, process.env.ACCESS_TOKEN_SECRET, { expiresIn: '15s' });
    User.update({ access_token: accessToken }, { where: { username: user.username } }).then((resp) => {
        console.log(resp);
        if (resp != 1) {
            res.status(403).send({ error: "Failed to update access token" });
        }
    }).catch((err) => {
        res.status(500).send({ error: err.message });
    });
    return accessToken;
}

exports.login = async function (req, res) {
    const username = req.body.username;
    const accessToken = await module.exports.generateAccessToken(req,res);
    const refreshToken = generateRefreshToken(username);
    return res.status(200).send({ message: "You have successfully login!", refreshToken: refreshToken, accessToken: accessToken });
}
Miracle Hades
  • 146
  • 2
  • 10
  • Your `User.update()` function is asynchronous and thus `return accessToken` does NOT return a value from your `generateAccessToken()` function. It just returns a value from the callback function. Also, it is a troublesome design to handle results and errors at two different levels as `exports.login()` has no way to detect errors or know that an error response has already been sent. Your general fix should be to return the promise from `generateAccessToken()` that you already have and then use that promise to get the value inside of your `login()` function. – jfriend00 Dec 05 '20 at 08:59
  • @jfriend00 pls check the update code but it might cause logical issue for the generateAccessToken() method when the update validation is not satisfied. – Miracle Hades Dec 05 '20 at 09:23

1 Answers1

0

You are not returning accessToken generateAccessToken. You return it in the then-part of User.update but that doesn’t mean it becomes the return value of generateAccesToken.

ilbrando
  • 65
  • 5
  • the generateAccessToken should only return the accessToken when the update is executed successfully. If follow your statement, the fail update operation might also leads return state triggered. – Miracle Hades Dec 05 '20 at 09:19