I have some cookie verification at the server side using nodejs . Every time I get a
get
request for a URL I fire theisAuthenticated
method to check and verify if the user is authenticated or not. The problem is that I am using a flag variable like shown below .
function isAuthenticated(req , res)
{
let flag_auth = 0 ;
verifyIdToken(mytoken).then(user=>{
console.log("User is signed in ") ;
flag_auth = 1 ;
})
.catch(error=>{
console.log("User not signed in yet ! ")
})
return flag_auth ;
}
Let say VerifyIDToken is a async call to a server (firebase) which returns a promise . Since this is a async call , my returned flag_auth will always be 0 since the ayanc call takes some time to complete by that time my flag_auth would have returned back to the caller.
if(isAuthenticated(req, res))
{
console.log('login get from Signed in user ! ') ;
}
else{
console.log("get request from not AUthenticated user ! ") ;
}
So everytime isAuthenticated returns a false value . What method can I use under such conditions ? If I need to implement something with promises , please explain with a short code snippet on how to go about it ?