From 8f39a1403d3c94f3d51693e41843750352408cba Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Thu, 29 Mar 2018 08:32:54 +0100 Subject: Address numerous errors in test suites Many errors still remian. --- src/auth.ts | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'src/auth.ts') diff --git a/src/auth.ts b/src/auth.ts index 368f130..8a6b248 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -38,6 +38,7 @@ const configStrategy: () => void = (): void => { * * @param req The Express http request. * @param res The Express http response. + * @param next The next Express middleware. */ const checkErr: Middleware = ( req: Request, res: Response, next: NextFunction @@ -89,9 +90,10 @@ const authenticate: Middleware = ( * * @param req The Express http request. * @param res The Express http response. + * @param next The next Express middleware - only used for errors. */ const genToken: Middleware = async( - req: Request, res: Response + req: Request, res: Response, next: NextFunction ): Promise => { const user: User | null = await User.findOne({ where: { @@ -102,7 +104,11 @@ const genToken: Middleware = async( if (user === null) { // Whoa, what an error! - return; + return next(new RequestError( + 400, + 'invalid_grant', + 'Invalid username' + )); } const token: string = jwt.sign( @@ -155,6 +161,22 @@ export class AuthRouter { } } +interface UnauthorizedError { + name: 'UnauthorizedError'; + message: string; + code: string; + status: number; +} + +const isUnauthorizedError: (item: object) => item is UnauthorizedError = ( + item: object +): item is UnauthorizedError => { + return (item as Partial).name === 'UnauthorizedError' + && (item as Partial).message !== undefined + && (item as Partial).code !== undefined + && (item as Partial).status !== undefined; +}; + /** * Error-handling middleware used to handle authentication errors. * @@ -168,10 +190,11 @@ export class AuthRouter { */ export const unauthorisedErr: Errorware = (err: Error, req: Request, res: Response, next: NextFunction): void => { - if (err.name === 'UnauthorizedError') { + if (isUnauthorizedError(err)) { return next(new RequestError( - 401, 'not_authorised', - 'user does not have correct authorisation for task' + err.status, + err.code, + err.message )); } next(err); -- cgit v1.2.1