I'm working on a REST API in node.js and I'm encountering a problem when trying to pass a value from one module to another, I'm rather new to node.js there is something I'm definitely missing something here...
I have a controller and a "service" module for authentication which uses JWT. What I'm trying to do is pass a user object to the auth service in order to obtain the JWT and pass it back to the controller in order to build the response.
In the service I get the JWT with no problems but I cannot pass it back... the return param;
does not work in this case.
Here is the function from my controller
exports.login = (req, res) => {
const email = req.body.email;
const password = req.body.password;
Users.findOne({ 'email': email, 'password': password }, (err, user) => {
if (err) {
res.json({ err });
} else {
if (typeof user !== null) {
var token = authService.generateToken(user);
res.json({
token
});
} else {
res.json({
status: 'error',
message: 'User not found!'
});
}
}
})
};
And here is the function from my authService (which is inclunded in the controller)
exports.generateToken = function(user) {
jwt.sign({ user }, params.secret, { expiresIn: params.expires }, (err, token) => {
if (err)
return err;
console.log(token);
return token;
});
}
Now that console.log(token)
from the authService
returns the valid JWT in the console, but in the controller
I get nothing back.
Can someone please help me with this?
Thanks!