| 1 | import { NextFunction, Request, Response } from 'express'; |
| 2 | |
| 3 | import { |
| 4 | HttpException, |
| 5 | HttpStatus, |
| 6 | Injectable, |
| 7 | NestMiddleware, |
| 8 | } from '@nestjs/common'; |
| 9 | |
| 10 | import { AuthService } |
| 11 | import { User } |
| 12 | |
| 13 | @Injectable() |
| 14 | export class AuthMiddleware |
| 15 | constructor(private readonly |
| 16 | async use( |
| 17 | req: Request |
| 18 | auth: Partial |
| 19 | }, |
| 20 | res: Response |
| 21 | next: NextFunction |
| 22 | ) { |
| 23 | try { |
| 24 | const token = |
| 25 | if (!token) |
| 26 | req.auth = |
| 27 | token.startsWith('Bearer |
| 28 | ); |
| 29 | next(); |
| 30 | } catch (e) |
| 31 | throw new HttpException( |
| 32 | 'Missing or Expired Token', |
| 33 | HttpStatus.UNAUTHORIZED, |
| 34 | ); |
| 35 | } |
| 36 | } |
| 37 | } |