Skip to content

Instantly share code, notes, and snippets.

@WoolDoughnut310
Created October 28, 2022 15:06
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save WoolDoughnut310/45caadf48b7f405a0ded66f2c7ae82cd to your computer and use it in GitHub Desktop.
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,
];
@dotNaimlyss
Copy link

where did that "../types" file come from ?

@WoolDoughnut310
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment