Sunday 12 March 2017

Nodejs how to block the actual endpoint that was requested from an .all endpoint?

I've got an .all endpoint which gets called for all endpoints connected to accounts. What I want to do in it is check a token and then if it's true, progress to the endpoint that was actually requested.

I thought next() was supposed to do this but the other requests are being called regardless of the token being valid or not.

What am I doing wrong?

router.all(['/accounts', '/accounts/*'], (req, res, next) => {

  admin.auth().verifyIdToken(req.headers.authorization)
  .then((token) => {

    // TODO: Fix so that this actually works, now the other routes are being called regardless
    next();
  }).catch((error) => {

    res.send({
      error: error,
      data: {
        account: null,
        message: 'Invalid token',
        status: 401
      }
    });
  });
});

router.post('/accounts', (req.res, next) => {});
router.post('/accounts/:uid', (req.res, next) => {});

How can I make sure that the .all endpoint only continues to either /accounts or /accounts/:uid if the verifyToken function is successful? Apparently next() doesn't do anything in terms of blocking consecutive requests.



via Chrillewoodz

No comments:

Post a Comment