const bcrypt = require('bcryptjs'); class Authenticator { constructor(database) { this.database = database; }; getAuthenticatedUser(req, callback) { if (req.session.userId) { let collection = this.database.collection('users'); collection.findOne({ id: req.session.userId }, {}, function (dbErr, dbRes) { if (dbErr === null) { if (dbRes === null) { callback(null); } else { callback(dbRes); } } else { callback(null); } }); } else { callback(null); } }; withUser(req, res, callback) { this.getAuthenticatedUser(req, (user) => { if (user) callback(user); else res.status(401).send({ code: 'auth error', message: 'user not logged in.' }); }); }; withAdmin(req, res, callback) { this.withUser(req, res, (user) => { if (user.role === 'Admin') callback(user) else res.status(403).send({ code: 'auth error', message: 'user is not Admin.' }); }); }; }; module.exports = Authenticator;