0

I have some cookie verification at the server side using nodejs . Every time I get a get request for a URL I fire the isAuthenticated 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 ?

Natesh bhat
  • 7,780
  • 5
  • 52
  • 90
  • You can't get around the nature of async code. You need to test whether or not they authenticated in `then()`, async/await, or a callback and react accordingly. You can't test in the main event loop with `if`. – Mark Dec 30 '17 at 03:28
  • `isAuthenticated = () => verifyIdToken(mytoken).then(user => 1, error => 0);` and then `isAuthenticated().then(flag_auth => console.log(flag_auth? "foo": "bar"))` – Thomas Dec 30 '17 at 03:36

1 Answers1

0

When dealing with asynchronicity, you need to make use of callbacks because only they will have access to the data that is retrieved in the future (asynchronously). I would try to restructure your application into 3 discrete parts:

/**
 * 1. check if the user is verified
 * this function accepts two callbacks on top of the user token.
 * one callback will be executed if the user is verified (2) and the other will be executed if the user is not (3)
 */
const checkIfUserIsVerified = (userToken, doThingsIfUserIsVerified, doThingsIfUserIsNotVerified) => {
  verifyIdToken(userToken)
    .then(doThingsIfUserIsVerified)
    .catch(doThingsIfUserIsNotVerified)
}

/**
 * 2. do things if the user is verified after checking
 */
const userIsVerified = user => {
  // this function has access to the user that verifyIdToken returns
  console.log('this function has access to the verified user: ', user)
}

/**
 * 3. do things if the user is not verified after checking
 */
const userIsNotVerified = error => {
  // this function has access to the error that was thrown when verifyIdToken tried verifying userToken
  console.error(error)
}

// now you can put everything together with mytoken (or whatever token you want to verify):
checkIfUserIsVerified(mytoken, userIsVerified, userIsNotVerified)

EDIT: here's a simpler and even more readable alternative to checkIfUserIsVerified using ES2017's async / await

const checkIfUserIsVerified = async (userToken, doThingsIfUserIsVerified, doThingsIfUserIsNotVerified) => {
  try {
    const user = await verifyIdToken(userToken)
    doThingsIfUserIsVerified(user)
  } catch(error) {
    doThingsIfUserIsNotVerified(error)
  }
}
feihcsim
  • 1,138
  • 2
  • 14
  • 25