I have this function that is supposed to authenticate a user and return a WT token:
async function authenticate({username, password}) {
userModel.find({username: username, password: password}, (err, doc) => {
if (doc[0]) {
const user = {
id: doc[0]._id,
username: doc[0].username,
password: doc[0].password,
firstName: doc[0].firstName,
lastName: doc[0].lastName,
role: doc[0].role
};
const token = jwt.sign({sub: user.id, role: user.role}, config.secret);
const {password, ...userWithoutPassword} = user;
return {
...userWithoutPassword,
token
};
}
});
}
userModel is a mongoose model. The return statement at the end seems not to return anything but doing console.log inside it, the values are filled correctly. Am I missing something? I am at the first attempts with node so if anything is not clear please ask.
I checked out the suggested previous solution but I don't understand how to apply it to my specific case.