I have the following function
const refresh = async req => {
const authorization = req.headers['authorization'];
if (!authorization) throw new Error("You need to login");
accesstoken = authorization.split(' ')[1];
//..... a lot of code
}
module.exports = {
refresh
}
And then I call this in another file
function requireLogin(req,res,next){
try {
const userId = isAuth(req)
if (userId !== null) {
next();
}
} catch (err) {
if(err.message == "jwt expired"){
console.log("jwt was expired")
async function result(){
console.log("inside result function")
var data = await refresh(req)
console.log(data) //undefined
}
result();
}
res.send({
error: `${err.message}`
})
}
}
So its basically not awaiting my function. If I console.log() the desired result inside my function, I can see it happening properly, but well i cant log it in the main file, since its not awaiting.
The syntax seems fine to me i dont know.
EDIT: This is the whole code of refresh
const refresh = async req => {
const authorization = req.headers['authorization'];
if (!authorization) throw new Error("You need to login");
accesstoken = authorization.split(' ')[1];
var userId = verify(accesstoken, process.env.ACCESS_TOKEN_SECRET, { ignoreExpiration: true })
userId = userId.userId
var token = ""
db.query("SELECT * FROM user_permisson WHERE user_id='" + userId + "'").then(function (data) {
var permited = [];
console.log("permisos de usuario")
delete data[0].user_id
for (var key in data[0]) {
if (data[0].hasOwnProperty(key)) {
if (data[0][key] === true) {
permited.push(key)
}
console.log(key + " -> " + data[0][key]);
}
}
console.log(permited)
//CASE 1(Front end): this is for the case when its coming from rect and is unable to read the private cookie, so it received an encrypted id that will identify the user so we are able to get its refresh token, client to server connection
//now the we need to grab the refreshtoken of the user knowing its id
db.query("SELECT * FROM users WHERE id='" + userId + "'").then(function (data) {
var user = data;
token = user[0].refreshtoken;
var id = user[0].id;
if (!token) return { accesstoken: '' };
let payload = null;
try {
payload = verify(token, process.env.REFRESH_TOKEN_SECRET);
} catch (err) {
return { accesstoken: '' };
}
user = "";
db.query("SELECT * FROM users WHERE id='" + id + "'").then(function (data) {
user = data;
if (!user) return { accesstoken: '' };
//if user exists check if refreshtoken exist on user
if (user[0].refreshtoken !== token) {
return { accesstoken: '' }
}
//if token exist create a new Refresh and Accestoken
const accesstoken = createAccessToken(user[0].id, permited);
const refreshtoken = createRefreshToken(user[0].id);
db.query("UPDATE users SET refreshtoken = '" + refreshtoken + "' WHERE id = '" + user[0].id + "';").then(function (data) {
// sendRefreshToken(res, refreshtoken); //unnecesary
console.log(accesstoken)
return { accesstoken }; //THIS IS THE RETURN I WANT TO HAPPEN <---------------------------------------
}).catch(function (error) {
console.log("ERROR: ", error)
})
}).catch(function (error) {
console.log("ERROR: ", error)
return error;
})
})
}) }