<commit_before>"use strict";

/**
 * Handle the user
 * @example
    user: {
        reset: ''
    }
 */

module.exports = function (config, libraries, services) {
    var app = services.app,
        db = services.db,
        password = services.password,
        send = services.send,
        validate = services.validate;

    app.post(
        '/register',
        [
            validate.email('email'),
            validate.password('password')
        ],
        function (req, res) {
            password.hash(req.body.password, function (passwordhash) {
                var user = {
                    email: req.body.email,
                    passwordhash: passwordhash
                };
                db.users.insert(user, function (err) {
                    if (err) {
                        res.json('email already in use');
                    }
                    var session = {
                        user_id: user._id
                    };
                    db.sessions.insert(session, function () {
                        res.json(session._id);
                    });
                });
            });
        }
    );

    app.post(
        '/login',
        [],
        function (req, res) {
            var query = { email: req.body.email };
            db.users.findOne(query, function (err, user) {
                if (!user) {
                    res.json('unknown email');
                    return;
                }
                password.verify(req.body.password, user.passwordhash, function (verify) {
                    if (!verify) {
                        res.json('wrong password');
                        return;
                    }
                    var session = {
                        user_id: user._id
                    };
                    db.sessions.insert(session, function () {
                        res.json(session._id);
                    });
                });
            });
        }
    );

    app.post(
        '/forget',
        function (req, res) {
            var query = { email: req.body.email };
            db.users.findOne(query, function (err, user) {
                if (!user) {
                    res.json('unknown email');
                    return;
                }
                var forget = {
                    user_id: user._id
                };
                db.forgets.insert(forget, function () {
                    send({
                        To: user.email,
                        Subject: 'Password forget',
                        TextBody: config.reset.replace('%token%', forget._id)
                    });
                });
                res.json();
            });
        }<commit_msg>Use the referrer for the password forget e-mail<commit_after>"use strict";

/**
 * Handle the user
 * @example
    user: {
        reset: ''
    }
 */

module.exports = function (config, libraries, services) {
    var app = services.app,
        db = services.db,
        password = services.password,
        send = services.send,
        validate = services.validate;

    app.post(
        '/register',
        [
            validate.email('email'),
            validate.password('password')
        ],
        function (req, res) {
            password.hash(req.body.password, function (passwordhash) {
                var user = {
                    email: req.body.email,
                    passwordhash: <PASSWORD>
                };
                db.users.insert(user, function (err) {
                    if (err) {
                        res.json('email already in use');
                    }
                    var session = {
                        user_id: user._id
                    };
                    db.sessions.insert(session, function () {
                        res.json(session._id);
                    });
                });
            });
        }
    );

    app.post(
        '/login',
        [],
        function (req, res) {
            var query = { email: req.body.email };
            db.users.findOne(query, function (err, user) {
                if (!user) {
                    res.json('unknown email');
                    return;
                }
                password.verify(req.body.password, user.passwordhash, function (verify) {
                    if (!verify) {
                        res.json('wrong password');
                        return;
                    }
                    var session = {
                        user_id: user._id
                    };
                    db.sessions.insert(session, function () {
                        res.json(session._id);
                    });
                });
            });
        }
    );

    app.post(
        '/forget',
        function (req, res) {
            var query = { email: req.body.email };
            db.users.findOne(query, function (err, user) {
                if (!user) {
                    res.json('unknown email');
                    return;
                }
                var forget = {
                    user_id: user._id
                };
                db.forgets.insert(forget, function () {
                    send({
                        To: user.email,
                        Subject: 'Password forget',
                        TextBody: config.reset.replace('%token%', forget._id).replace('%referrer%', req.body.referrer)
                    });
                });
                res.json();
            });
        }
{'fraction_non_alphanumeric': 0.113245153107447, 'fraction_numerical': 0.0, 'mean_word_length': 0.8988648090815273, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '7171570', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1267, 'n_words': 256}