Significant API progress
This commit is contained in:
parent
ea72d194c2
commit
01227bb28f
27 changed files with 2378 additions and 0 deletions
30
backend/src/app/middleware/UserInjection.ts
Normal file
30
backend/src/app/middleware/UserInjection.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import { NextFunction, Request, Response } from "express";
|
||||
import * as jwt from "jsonwebtoken";
|
||||
import moment from "moment";
|
||||
import Users from "../models/User";
|
||||
|
||||
export default () => {
|
||||
return async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const token = req.header("auth-token");
|
||||
if (!token) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const { _id, expiresAt } = jwt.verify(
|
||||
token,
|
||||
process.env.JWT_SECRET as string
|
||||
) as { _id: string; expiresAt: string };
|
||||
|
||||
if (moment(moment()).isAfter(expiresAt)) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const user = await Users.findById(_id);
|
||||
req.user = user;
|
||||
next();
|
||||
} catch (err) {
|
||||
return res.status(400).json({ error: `Invalid Token: ${err}` });
|
||||
}
|
||||
};
|
||||
};
|
||||
Loading…
Reference in a new issue