diff options
| -rw-r--r-- | src/app.ts | 4 | ||||
| -rw-r--r-- | src/auth.ts | 29 | ||||
| -rw-r--r-- | src/errors/errorware.ts | 24 |
3 files changed, 34 insertions, 23 deletions
@@ -4,7 +4,7 @@ import * as expressJwt from 'express-jwt'; import * as logger from 'morgan'; import * as passport from 'passport'; -import { default as authRouter } from './auth'; +import { unauthorisedErr, default as authRouter } from './auth'; import { RequestError, errorHandler } from './errors/errorware'; import FileRouter from './FileRouter'; @@ -65,7 +65,7 @@ class App { } private errorware(): void { - // TODO: this.express.use(authRouter.unauthorisedErr); + this.express.use(unauthorisedErr); this.express.use(errorHandler); } diff --git a/src/auth.ts b/src/auth.ts index b40cddb..083357e 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -7,6 +7,8 @@ import * as passport from 'passport'; import { Strategy } from 'passport-local'; import { default as User } from './db/model/User'; +import { Errorware } from './errors/errorware'; + export class AuthRouter { public router: Router; @@ -74,19 +76,6 @@ export class AuthRouter { next(); } - // Handle error if user is unauthorised - public unauthorisedErr( - err: Error, req: Request, res: Response, next: NextFunction - ): void { - if (err.name === 'UnauthorizedError') { - next(new RequestError( - 401, 'not_authorised', - 'user does not have correct authorisation for task' - )); - } - next(); - } - // Middleware to check privileges - admin public isAdmin(req: Request, res: Response, next: NextFunction): void { if (!req.user.admin) { @@ -127,6 +116,20 @@ export class AuthRouter { } } +// Handle error if user is unauthorised +export const unauthorisedErr: Errorware = + (err: Error, req: Request, res: Response, next: NextFunction): void => { + console.log('fffff'); + if (err.name === 'UnauthorizedError') { + console.log('efewfwf'); + return next(new RequestError( + 401, 'not_authorised', + 'user does not have correct authorisation for task' + )); + } + next(err); +}; + const authRoutes: AuthRouter = new AuthRouter(); authRoutes.init(); diff --git a/src/errors/errorware.ts b/src/errors/errorware.ts index 45a76b3..e2355de 100644 --- a/src/errors/errorware.ts +++ b/src/errors/errorware.ts @@ -10,6 +10,13 @@ export interface ErrorResponseBlock { } export class RequestError extends Error { + public static is(test: Object): test is RequestError { + const err: RequestError = test as RequestError; + return err.code !== undefined + && err.name !== undefined + && err.responseBlock !== undefined; + } + public code: number; public userMessage?: string; // tslint:disable-next-line:no-any @@ -41,16 +48,17 @@ export class RequestError extends Error { } } -type Errorware = +export type Errorware = (err: Error, req: Request, res: Response, next: NextFunction) => void; export const errorHandler: Errorware = (err: Error, req: Request, res: Response, next: NextFunction): void => { - if ((err as RequestError).responseBlock) { - const rerr: RequestError = err as RequestError; - res.status(rerr.code) - .json(rerr.responseBlock()); - } else { - res.send(err); - } + console.log(err); + if (RequestError.is(err)) { + console.log('fewkuihfklsfv'); + res.status(err.code) + .json(err.responseBlock()); + } else { + res.send(err); + } }; |
