Fixed members auth middleware

no-issue

The JWT library we used does not throw an error which can be used by
Ghost. So we need to catch and wrap it in our own errors from
@tryghost/errors.
This commit is contained in:
Fabien O'Carroll 2021-05-24 10:42:05 +01:00
parent 2b5e22524b
commit f4c40249ce

View File

@ -1,4 +1,5 @@
const jwt = require('express-jwt'); const jwt = require('express-jwt');
const {UnauthorizedError} = require('@tryghost/errors');
const membersService = require('../../members'); const membersService = require('../../members');
const config = require('../../../../shared/config'); const config = require('../../../../shared/config');
@ -37,8 +38,19 @@ module.exports = {
} }
})); }));
} }
return function (req, res, next) { return async function (req, res, next) {
UNO_MEMBERINO.then(fn => fn(req, res, next)).catch(next); try {
const middleware = await UNO_MEMBERINO;
middleware(req, res, function (err, ...rest) {
if (err && err.name === 'UnauthorizedError') {
return next(new UnauthorizedError({err}), ...rest);
}
return next(err, ...rest);
});
} catch (err) {
next(err);
}
}; };
} }
}; };