2023-11-01 02:00:36 +00:00
|
|
|
const db = require("../data/database");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function auth (req, res, next) {
|
|
|
|
const user = req.session.user;
|
|
|
|
const isAuth = req.session.isAuthenticated;
|
|
|
|
|
2023-11-04 22:08:12 +00:00
|
|
|
if (!user || !isAuth) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2023-11-01 02:00:36 +00:00
|
|
|
const userDoc = await db.getDb().collection('users').findOne({_id: user.id})
|
|
|
|
|
2023-11-04 00:32:44 +00:00
|
|
|
|
2023-11-04 22:08:12 +00:00
|
|
|
const isAdmin = userDoc.isAdmin;
|
|
|
|
|
|
|
|
res.locals.user = userDoc;
|
|
|
|
res.locals.isAuth = isAuth;
|
|
|
|
res.locals.isAdmin = isAdmin;
|
2023-11-04 00:32:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-11-01 02:00:36 +00:00
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = auth
|