-
Notifications
You must be signed in to change notification settings - Fork 0
/
guard.ts
33 lines (30 loc) · 933 Bytes
/
guard.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { NextFunction, Request, Response } from 'express'
import { State } from '../State'
import { PASSPORT_NAME } from '../utils/enums'
import { ErrorBuilder } from '../utils/ErrorBuilder'
import { customTFunction } from '../utils/translations'
import { ID, IUser } from '../types/interfaces'
/**
* Guard middleware for password reset.
* Needs to be a function, since passport is provided after import
* @param req
* @param res
* @param next
*/
export default function guard(req: Request, res: Response, next: NextFunction) {
State.getInstance().passport.authenticate(PASSPORT_NAME.JWT_PASSWORD_RESET, (err: any, userData: IUser<ID>) => {
try {
if (err) {
return next(err)
}
if (!userData) {
const t = req.t ?? customTFunction
throw new ErrorBuilder(401, t('error:Password reset token is invalid'))
}
req.user = userData
return next()
} catch (e) {
return next(e)
}
})(req, res)
}