0

fist of all sorry for my bad english. I'm learning JavaScript, and i have a little problem with the Google O2Auth, in my node server i try to stop the code when the google token is invalid or expired, but the "return" inside the catch send the http response and i can see it in Postman but the code continue the execution and i have to put a awfull if to stop the code outside the catch.

Here is the Code

// The google code for NodeJS
async function verify(token) {
  var ticket = await client.verifyIdToken({
      idToken: token,
      audience: GOOGLE_CLIENT_ID
  });

  var payload = ticket.getPayload();

  return {
    nombre: payload.name,
    email: payload.email,
    img: payload.picture,
    google: true
  }
}

// My code
app.post('/google', async (req, res) => {
  var token = req.body.token;

  // Here is the problem, in the .catch i put a return 403... but the code don't stop and enter in the Usuario.FindOne.
  var googleUser = await verify(token).catch(err => {
    return res.status(403).json({
      ok: false,
      mensaje: 'Token de google inválido',
      errors: { message: 'Token de google inválido' }
    });
  });

  Usuario.findOne({ email: googleUser.email }, (err, usuario) => {
    if (err) {
      return res.status(500).json({
        ok: false,
        ...
        ...
        ...

Well the problem is that the code don't stop in the return "status 403", well after that function enter in the findOne function and brake the code if find any error...

No problem when the token is invalid because don't receive a email, but when the token is expired I receive a email, and my node server crashes!

The question is, I need to break the code in the verify(token).catch() how i can to this?

Thanks!

0

1 Answer 1

2

You only return from the callback function passed to the catch method in there. This doesn't stop anything else.

Use try/catch instead, where return will return out of the async function:

app.post('/google', async (req, res) => {
  var token = req.body.token;

  try {
    var googleUser = await verify(token);
  } catch (err) {
    res.status(403).json({
      ok: false,
      mensaje: 'Token de google inválido',
      errors: { message: 'Token de google inválido' }
    });
    return;
  }
  try {
    var usuario = await new Promise((resolve, reject) => {
      Usuario.findOne({ email: googleUser.email }, (err, usuario) => {
        if (err) reject(err);
        else resolve(usario);
      });
    });
  } catch(err) {
    return res.status(500).json({
      ok: false,
      …
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Bergi, i understand perfect this!!! the return in the callback stop the callback function but not the parent function! Works perfect!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.