Created
October 28, 2022 15:06
-
-
Save WoolDoughnut310/45caadf48b7f405a0ded66f2c7ae82cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { User } from "../db/types"; | |
import { expressjwt } from "express-jwt"; | |
import { NextFunction, Response } from "express"; | |
import getDb from "../db"; | |
import { Request } from "../types"; | |
import { ObjectId } from "mongodb"; | |
const injectUser = async (req: Request, res: Response, next: NextFunction) => { | |
const db = getDb(); | |
const users = db.collection<User>("user"); | |
if (req.auth.id) { | |
req.auth.id = new ObjectId(req.auth.id); | |
req.user = await users.findOne({ | |
_id: req.auth.id, | |
}); | |
console.log("req.user", req.user, req.auth.id); | |
} | |
next(); | |
}; | |
export default () => [ | |
expressjwt({ secret: process.env.JWT_SECRET, algorithms: ["HS256"] }), | |
injectUser, | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where did that "../types" file come from ?