Added U and D endpoints.

This commit is contained in:
2022-04-04 22:50:26 -05:00
parent ba566040b1
commit 7dd862e134
4 changed files with 176 additions and 69 deletions

View File

@@ -1,6 +1,7 @@
import MongoStore from "connect-mongo";
import session from "express-session";
import { mongooseDbName, mongoURI } from "../database/mongoose.js";
import userModel from "../schemas/userModel.js";
const sessionConf = {
secret: process.env.SESSION_SECRET || "super duper secret string.",
cookie: {
@@ -16,11 +17,12 @@ if (process.env.NODE_ENV === "production") {
}
export const userSession = session(sessionConf);
export function authenticationGuard(req, res, next) {
export async function authenticationGuard(req, res, next) {
if (req.session.userId) {
req.user = await userModel.findById(req.session.userId);
next();
} else {
res.sendStatus(401);
res.status(401).send("Not authorized.");
return;
}
}