(_?: never)
| 9 | import to from '@linen/utilities/await-to-js'; |
| 10 | |
| 11 | export default function jwtMiddleware(_?: never) { |
| 12 | return async (req: AuthedRequest, res: any, next: NextFunction) => { |
| 13 | const token = getToken(req); |
| 14 | if (!token) { |
| 15 | return next(new Unauthorized()); |
| 16 | } |
| 17 | |
| 18 | const [err, session] = await to(verifyToken(token)); |
| 19 | |
| 20 | if (err) { |
| 21 | expireSessionCookies({ req, res }); |
| 22 | return next(new Unauthorized()); |
| 23 | } |
| 24 | |
| 25 | if (!session) { |
| 26 | return next(new Unauthorized()); |
| 27 | } |
| 28 | |
| 29 | if (typeof session === 'object') { |
| 30 | const user = await UsersService.getUserById(session.data.id); |
| 31 | if (!user) { |
| 32 | return next(new Unauthorized()); |
| 33 | } |
| 34 | req.session_user = { ...user, exp: session.exp }; |
| 35 | } |
| 36 | |
| 37 | return next(); |
| 38 | }; |
| 39 | } |
no test coverage detected