<commit_before>'use strict';
var _ = rq('lodash'),
    jwt = rq('jsonwebtoken');
var users = [{
    id: 1,
    username: 'admin',
    password: '<PASSWORD>',
    roles: ['admin', 'registered']
}, {
    id: 2,
    username: 'user',
    password: '<PASSWORD>',
    roles: ['registered']
}];

function createToken(user) {
    return jwt.sign(_.omit(user, 'password'), process.env.SECRET || 'fapp-stack-secret', {
        expiresInMinutes: process.env.TOKENEXPIRATIONTIME || 1440
    });
}
module.exports.login = function(req, res) {
    if (!req.body.username || !req.body.password) {
        return res.status(400)
            .send("You must send the username and the password");
    }
    var user = _.find(users, {
        username: req.body.username
    });
    if (!user) {
        return res.status(401)
            .send("The username or password don't match");
    }
    if (user.password !== <PASSWORD>.body.password) {
        return res.status(401)
            .send("The username or password don't match");
    }
    res.status(201)
        .send({
            token: createToken(user)
        });
};
module.exports.signUp = function(req, res) {
    if (!req.body.username || !req.body.password) {
        return res.status(400)
            .send("You must send the username and the password");
    }
    if (_.find(users, {
        username: req.body.username
    })) {
        return res.status(400)
            .send("A user with that username already exists");
    }
    var profile = _.pick(req.body, 'username', 'password', 'extra');
    profile.id = _.max(users, 'id')
        .id + 1;
    profile.roles = ['registered'];
    users.push(profile);
    res.status(201)
        .send({
            token: createToken(profile)
        });
};
module.exports.newToken = function(req, res) {
    console.log(req.user);
    res.status(201)
        .send({
            token: createToken(req.user)
        });
};<commit_msg>added auth via mongoDB<commit_after>'use strict';
var _ = rq('lodash'),
    jwt = rq('jsonwebtoken'),
    User = rq('userModel');

function createToken(user) {
    return jwt.sign(_.omit(user, 'password'), process.env.SECRET || 'fapp-stack-secret', {
        expiresInMinutes: process.env.TOKENEXPIRATIONTIME || 1440
    });
}
module.exports.login = function(req, res) {
    if (!req.body.username || !req.body.password) {
        return res.status(400)
            .send("You must send the username and the password");
    }
    process.nextTick(function() {
        User.findOne({
            username: req.body.username
        })
            .select('+password')
            .exec(function(err, user) {
                if (err || !user) {
                    return res.status(401)
                        .send("The username or password don't match");
                }
                user.comparePassword(req.body.password, function(err, isMatch) {
                    if (isMatch && !err) {
                        res.status(201)
                            .send({
                                token: createToken(user)
                            });
                    } else {
                        return res.status(401)
                            .send("The username or password don't match");
                    }
                });
            });
    });
};
module.exports.signUp = function(req, res) {
    delete req.body.roles;
    if (!req.body.username || !req.body.password || !req.body.email) {
        return res.status(400)
            .send("You must send the username, password and email");
    }
    User.find({
        username: req.body.username
    })
        .exec(function(err, users) {
            if (users.length) {
                return res.status(400)
                    .send("A user with that username already exists");
            } else {
                var user = new User();
                user = _.extend(user, req.body);
                user.save(function(err) {
                    if (err) {
                        console.log(err);
                        return res.status(500)
                            .send("Something went wrong while creating your account");
                    } else {
                        return res.status(201)
                            .send({
                                token: createToken(user)
                            });
                    }
                });
            }
        });
};
module.exports.newToken = function(req, res) {
    res.status(201)
        .send({
            token: createToken(req.user)
        });
};
{'fraction_non_alphanumeric': 0.1384817326624371, 'fraction_numerical': 0.013563771603587836, 'mean_word_length': 1.5584778959149412, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '364838', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1272, 'n_words': 279}