{"size": 769, "ext": "js", "max_stars_repo_path": "middleware/basicAuth.js", "max_stars_repo_name": "shandeepc/Node-REST-Api-As-IIQ-Source", "max_stars_repo_stars_event_min_datetime": "2022-02-12T11:18:50.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-12T11:18:50.000Z", "max_issues_repo_path": "middleware/basicAuth.js", "max_issues_repo_name": "shandeepc/Node-REST-Api-As-IIQ-Source", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "middleware/basicAuth.js", "max_forks_repo_name": "shandeepc/Node-REST-Api-As-IIQ-Source", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 33.4347826087, "max_line_length": 86, "alphanum_fraction": 0.6397919376}
const expressBasicAuth = require('express-basic-auth');
const bcrypt = require('bcrypt');
const users = require('../model/users.json');
const logger = require('./logger');

const basicAuth = expressBasicAuth({
    authorizer: (username, password) => {
        const user = users.find(u => u.username === username);
        if(user) {
            const userMatches = expressBasicAuth.safeCompare(username, user.username);
            const passwordMatches = bcrypt.compareSync(password, user.password);
            return userMatches & passwordMatches;
        }
        return 0;
    },
    unauthorizedResponse: (req) => {
        logger.log(`Unauthorized ~~> basicAuthentication`, 'reqLog.txt');
        return `Unauthorized.`;
    }
});

module.exports = basicAuth;
