<commit_before>const Router = require('express').Router;
const router = Router();
const User = require('../models/user');
const tokenService = require('../auth/token-service');
const ensureAuth = require('../auth/ensure-auth')();
const analyze = require('../utils/analyze');
// const superagent = require('superagent');
const Event = require('../models/event');

function hasEmailAndPassword(req, res, next) {
    const { email, password } = req.body;
    if (!email || !password) {
        return next({
            code: 400,
            error: 'both email and password are required'
        });
    }
    next();
}

function checkUser(req, res, next) {
    analyze.verifyUser(req.body.email, req.connection.remoteAddress)
        .then(res => {
            if(res === 'terminate') {
                return next({
                    code: 401,
                    error: 'Access Denied'
                });
            }
            next();
        });
}

router
    .get('/verify', ensureAuth, (req, res) => {
        res.send({ valid: true });
    })

    .post('/signup', hasEmailAndPassword, checkUser, (req, res, next) => {
        const { email, password, name, role, status } = req.body;

        delete req.body.password;

        User.exists({ email })
            .then(exists => {
                if (exists) {
                    throw next({
                        code: 400,
                        error: 'the email provided is already in use'
                    });
                }

                let user = new User({ email, name, role, status });
                user.generateHash(password);
                user.save();

                // log event on sign up
                const event = new Event({
                    email: user.email,
                    type: 'signup',
                    user: user._id });
                event.save();

                return user;
            })
            .then(user => tokenService.sign(user))
            .then(token => res.send({ token }))
            .catch(next);
    })

    .post('/signin', hasEmailAndPassword, checkUser, (req, res, next) => {
        const { email, password } = req.body;
        delete req.body.password;

        User.findOne({ email })
            .then(user => {
                if (!user || !user.comparePassword(password)) {
                    throw next({
                        code: 401,
                        error: 'Invalid Login'
                    });
                }
                return user;
            })
            .then(user => {

                // log event on sign in
                const event = new Event({
                    email: user.email,
                    type: 'login', 
                    user: user._id });
                event.save();

                tokenService.sign(user);
            })
            .then(token => res.send({ token }))
            .catch(next);
    });

module.exports = router;<commit_msg>added level<commit_after>const Router = require('express').Router;
const router = Router();
const User = require('../models/user');
const tokenService = require('../auth/token-service');
const ensureAuth = require('../auth/ensure-auth')();
const analyze = require('../utils/analyze');
// const superagent = require('superagent');
const Event = require('../models/event');

function hasEmailAndPassword(req, res, next) {
    const { email, password } = req.body;
    if (!email || !password) {
        return next({
            code: 400,
            error: 'both email and password are required'
        });
    }
    next();
}

function checkUser(req, res, next) {
    analyze.verifyUser(req.body.email, req.connection.remoteAddress)
        .then(res => {
            if(res === 'terminate') {
                return next({
                    code: 401,
                    error: 'Access Denied'
                });
            }
            next();
        });
}

router
    .get('/verify', ensureAuth, (req, res) => {
        res.send({ valid: true });
    })

    .post('/signup', hasEmailAndPassword, checkUser, (req, res, next) => {
        const { email, password, name, role, status } = req.body;

        delete req.body.password;

        User.exists({ email })
            .then(exists => {
                if (exists) {
                    throw next({
                        code: 400,
                        error: 'the email provided is already in use'
                    });
                }

                let user = new User({ email, name, role, status });
                user.generateHash(password);
                user.save();

                // log event on sign up
                const event = new Event({
                    email: user.email,
                    type: 'signup',
                    user: user._id,
                    level: 'low'
                });
                event.save();

                return user;
            })
            .then(user => tokenService.sign(user))
            .then(token => res.send({ token }))
            .catch(next);
    })

    .post('/signin', hasEmailAndPassword, checkUser, (req, res, next) => {
        const { email, password } = req.body;
        delete req.body.password;

        User.findOne({ email })
            .then(user => {
                if (!user || !user.comparePassword(password)) {
                    throw next({
                        code: 401,
                        error: 'Invalid Login'
                    });
                }
                return user;
            })
            .then(user => {

                // log event on sign in
                const event = new Event({
                    email: user.email,
                    type: 'login', 
                    user: user._id,
                    level: 'low'
                });
                event.save();

                tokenService.sign(user);
            })
            .then(token => res.send({ token }))
            .catch(next);
    });

module.exports = router;
{'fraction_non_alphanumeric': 0.13437292634372927, 'fraction_numerical': 0.003981420039814201, 'mean_word_length': 1.2529895366218236, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '1502843', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1482, 'n_words': 382}