added suspending admin email to error screen
This commit is contained in:
@@ -29,6 +29,7 @@ api.use(async (ctx, next) => {
|
||||
} catch (err) {
|
||||
ctx.status = err.status || 500;
|
||||
let message = err.message || err.name;
|
||||
let error;
|
||||
|
||||
if (err instanceof Sequelize.ValidationError) {
|
||||
// super basic form error handling
|
||||
@@ -40,18 +41,21 @@ api.use(async (ctx, next) => {
|
||||
|
||||
if (message.match('Authorization error')) {
|
||||
ctx.status = 403;
|
||||
error = 'authorization_error';
|
||||
}
|
||||
|
||||
if (ctx.status === 500) {
|
||||
message = 'Internal Server Error';
|
||||
error = 'internal_server_error';
|
||||
ctx.app.emit('error', err, ctx);
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
ok: false,
|
||||
error: _.snakeCase(err.id || err.message),
|
||||
error: _.snakeCase(err.id || error),
|
||||
status: err.status,
|
||||
message,
|
||||
adminEmail: err.adminEmail ? err.adminEmail : undefined,
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
@@ -76,7 +76,8 @@ export default function auth() {
|
||||
}
|
||||
|
||||
if (user.isSuspended) {
|
||||
throw new UserSuspendedError();
|
||||
const suspendingAdmin = await User.findById(user.suspendedById);
|
||||
throw new UserSuspendedError({ adminEmail: suspendingAdmin.email });
|
||||
}
|
||||
|
||||
ctx.state.token = token;
|
||||
|
||||
@@ -159,8 +159,10 @@ describe('Authentication middleware', async () => {
|
||||
|
||||
it('should return an error for suspended users', async () => {
|
||||
const state = {};
|
||||
const admin = await buildUser({});
|
||||
const user = await buildUser({
|
||||
suspendedAt: new Date(),
|
||||
suspendedById: admin.id,
|
||||
});
|
||||
const authMiddleware = auth();
|
||||
|
||||
@@ -179,6 +181,7 @@ describe('Authentication middleware', async () => {
|
||||
expect(e.message).toEqual(
|
||||
'Your access has been suspended by the team admin'
|
||||
);
|
||||
expect(e.adminEmail).toEqual(admin.email);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user