<commit_before>const flash = require('connect-flash');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const bcrypt = require('bcrypt');
const mongoose = require('mongoose');
const log = require('./logger');

const exp = module.exports = {};

exp.passport = passport;
exp.flash = flash;
exp.LocalStrategy = LocalStrategy;

exp.connect2DB = function(UserModel, passport){
    const User = UserModel;
    passport.use(new LocalStrategy(
        function(username, password, cb) {
            console.log("Authenticating: "+username);
            User.findOne({ username: username, recordStatus:'active'}).then(
                function(user) {
                    if (!user) {
                        log.WARN("LOGIN FAILED: user not found. user="+username);
                        return cb(null, false);
                    }
                    console.log('Found user '+user.username);
                    bcrypt.compare(password, user.passwordHash, function(err, isSame) {
                            if (err) return cb(err);
                            if (!isSame) {
                                log.WARN("LOGIN FAILED: wrong password. user="+username);
                                return cb(null, false);
                            }
                            log.INFO("LOGIN OK: User="+username);
                            return cb(null, user);
                        });
                },
                function (err) { return cb(err); });
        }
    ));

    passport.serializeUser(function(user, cb) {
        cb(null, user.id);
    });<commit_msg>if superAdmin then admin<commit_after>const flash = require('connect-flash');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const bcrypt = require('bcrypt');
const mongoose = require('mongoose');
const log = require('./logger');

const exp = module.exports = {};

exp.passport = passport;
exp.flash = flash;
exp.LocalStrategy = LocalStrategy;

exp.connect2DB = function(UserModel, passport){
    const User = UserModel;
    passport.use(new LocalStrategy(
        function(username, password, cb) {
            console.log("Authenticating: "+username);
            User.findOne({ username: username, recordStatus:'active'}).then(
                function(user) {
                    if (!user) {
                        log.WARN("LOGIN FAILED: user not found. user="+username);
                        return cb(null, false);
                    }
                    console.log('Found user '+user.username);
                    bcrypt.compare(password, user.passwordHash, function(err, isSame) {
                            if (err) return cb(err);
                            if (!isSame) {
                                log.WARN("LOGIN FAILED: wrong password. user="+username);
                                return cb(null, false);
                            }
                            log.INFO("LOGIN OK: User="+username);

                            if (user.isSuperAdmin) user.isAdmin = true;
                        
                            return cb(null, user);
                        });
                },
                function (err) { return cb(err); });
        }
    ));

    passport.serializeUser(function(user, cb) {
        cb(null, user.id);
    });
{'fraction_non_alphanumeric': 0.12250966399048469, 'fraction_numerical': 0.0005947071067499256, 'mean_word_length': 1.5777777777777777, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '593126', 'n_tokens_mistral': 845, 'n_tokens_neox': 818, 'n_words': 189}