From 76148b27f7501bac061992136852be4303370c8d Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Tue, 1 Jun 2021 01:36:53 +0200 Subject: refactor API errors to standard error format --- server/middlewares/auth.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'server/middlewares/auth.ts') diff --git a/server/middlewares/auth.ts b/server/middlewares/auth.ts index f38373624..176461cc2 100644 --- a/server/middlewares/auth.ts +++ b/server/middlewares/auth.ts @@ -16,11 +16,11 @@ function authenticate (req: express.Request, res: express.Response, next: expres .catch(err => { logger.warn('Cannot authenticate.', { err }) - return res.status(err.status) - .json({ - error: 'Token is invalid.', - code: err.name - }) + return res.fail({ + status: err.status, + message: 'Token is invalid', + type: err.name + }) }) } @@ -52,7 +52,12 @@ function authenticatePromiseIfNeeded (req: express.Request, res: express.Respons // Already authenticated? (or tried to) if (res.locals.oauth?.token.User) return resolve() - if (res.locals.authenticated === false) return res.sendStatus(HttpStatusCode.UNAUTHORIZED_401) + if (res.locals.authenticated === false) { + return res.fail({ + status: HttpStatusCode.UNAUTHORIZED_401, + message: 'Not authenticated' + }) + } authenticate(req, res, () => resolve(), authenticateInQuery) }) -- cgit v1.2.3