Metzploreur/middlewares/auth-middleware.js
clement callaert 1e373c1986 mongodb
2023-11-04 20:21:05 +01:00

25 lines
No EOL
455 B
JavaScript

const db = require("../data/database");
async function auth (req, res, next) {
const user = req.session.user;
const isAuth = req.session.isAuthenticated;
const userDoc = await db.getDb().collection('users').findOne({_id: user.id})
if (userDoc && userDoc.isAdmin) {
const isAdmin = userDoc.isAdmin;
res.locals.user = userDoc;
res.locals.isAuth = isAuth;
res.locals.isAdmin = isAdmin;
}
next();
}
module.exports = auth