diff options
| author | Tom Harley | 2018-03-03 16:42:16 +0000 |
|---|---|---|
| committer | Tom Harley | 2018-03-03 16:42:16 +0000 |
| commit | 652d328dc1d6605687cbd6858a3d27da1c126f7e (patch) | |
| tree | 93bab0513665714c3f20948e5f39428dea7c9ca0 /src/auth.ts | |
| parent | Fix minor things (diff) | |
| download | medicine-652d328dc1d6605687cbd6858a3d27da1c126f7e.tar.gz medicine-652d328dc1d6605687cbd6858a3d27da1c126f7e.zip | |
Implement handling of unauthorised requests
Diffstat (limited to 'src/auth.ts')
| -rw-r--r-- | src/auth.ts | 29 |
1 files changed, 16 insertions, 13 deletions
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(); |
