-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforgot-middleware.ts
More file actions
27 lines (24 loc) · 917 Bytes
/
forgot-middleware.ts
File metadata and controls
27 lines (24 loc) · 917 Bytes
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
import { NextFunction, Request, Response } from "express-serve-static-core";
import jwt from "jsonwebtoken";
export const verifyPasswordReset = async (req: Request, res: Response, next: NextFunction) => {
try {
const { token } = req.body;
if (!token) {
return res.status(401).json({ message: "UnAuthorized" });
}
jwt.verify(token, process.env.JWT_SECRET!, (err:any, decoded:any) => {
if (err) {
if (err.name === 'TokenExpiredError') {
return res.status(401).send({ error: "Token has expired" });
} else {
return res.status(401).send({ error: "Invalid token" });
}
}
req.user = decoded as string;
next();
});
} catch (err) {
console.log(err)
return res.status(500).send('Internal Server Error');
}
}