const fs = require('fs');
const path = require('path');

const checkSession = (req, res, next) => {
    const configPath = path.join(__dirname, '..', 'config', 'credentials.json');
    const configFile = fs.readFileSync(configPath);
    const config = JSON.parse(configFile);

    const userEmail = req.cookies.email;

    if (req.path === '/login') {
        next();
    } else {
        if (userEmail && decodeURIComponent(userEmail) === config.email) {
            next();
        } else {
            res.redirect('/login');
        }
    }
}

module.exports = checkSession
