<commit_before>const express = require('express');
const router = express.Router();
const db = require('../config/database-config');

const bodyParser = require('body-parser');
const jsonParser = bodyParser.json({ type: 'application/*+json'});


router.get('/', function(req, res) {
    res.sendfile('./views/index.html');
    // db.query('SELECT * FROM sec.user', null, (err, dbResults) => {
    //     if (err) {
    //         return next(err);
    //     }
    //     res.send(dbResults.rows);
    // });
});

router.get('/login', function(req, res) {
    res.sendfile('./views/login.html');
});

// process the login form
// app.post('/login', do all our passport stuff here);


// show the signup form
router.get('/signup', function(req, res) {
    res.sendfile('./views/signup.html');
});

// process the signup form
// app.post('/signup', do all our passport stuff here);

router.post('/signup', jsonParser, function(req, ress, next){

    req.checkBody ('firstname','First name is required').notEmpty();
    req.checkBody ('lastname','Last name is required').notEmpty();
    req.checkBody ('username','Username is required').notEmpty();
    req.checkBody ('username','Username must be between 4-20 charecters long').len(4, 20);
    req.checkBody ('password','<PASSWORD>').notEmpty();
    req.checkBody ('password','Password must be between 4-20 charecters long').len(4, 20);

    const errors = req.validationErrors();
    if(errors){
        return next(JSON.stringify(errors));
    }

    const firstName = req.body.firstname;
    const lastName  = req.body.lastname;
    const username  = req.body.username;
    const password  = <PASSWORD>;

    db.query('INSERT INTO sec.user (first_name, last_name, userXname, password_hash) VALUES ($1, $2, $3, $4)',
        [firstName, lastName, username, password], function(err, res){

        if(err) {
            return next(err);
        }

        ress.send('Success');
    });

});

// =====================================
// PROFILE SECTION =====================
// =====================================
// we will want this protected so you have to be logged in to visit
// we will use route middleware to verify this (the isLoggedIn function)
router.get('/profile', isLoggedIn, function(req, res) {
    // res.render('profile.ejs', {
    //     user : req.user // get the user out of session and pass to template
    // });

    res.send(200);
});

// =====================================
// LOGOUT ==============================
// =====================================
router.get('/logout', function(req, res) {
    req.logout();
    res.redirect('/');
});



// route middleware to make sure a user is logged in<commit_msg>- Added bcrypt module to hash new user's password into the databse.<commit_after>const express    = require('express');
const router     = express.Router();
const db = require('../config/database-config');
const bodyParser = require('body-parser');
const jsonParser = bodyParser.json({ type: 'application/*+json'});
const bcrypt     = require('bcrypt');


router.get('/', function(req, res) {
    res.sendfile('./views/index.html');
    // db.query('SELECT * FROM sec.user', null, (err, dbResults) => {
    //     if (err) {
    //         return next(err);
    //     }
    //     res.send(dbResults.rows);
    // });
});

router.get('/login', function(req, res) {
    res.sendfile('./views/login.html');
});

// process the login form
// app.post('/login', do all our passport stuff here);


// show the signup form
router.get('/signup', function(req, res) {
    res.sendfile('./views/signup.html');
});

// process the signup form
// app.post('/signup', do all our passport stuff here);

router.post('/signup', jsonParser, function(req, ress, next){

    req.checkBody ('firstname','First name is required').notEmpty();
    req.checkBody ('lastname','Last name is required').notEmpty();
    req.checkBody ('username','Username is required').notEmpty();
    req.checkBody ('username','Username must be between 4-20 charecters long').len(4, 20);
    req.checkBody ('password','<PASSWORD>').notEmpty();
    req.checkBody ('password','Password must be between 4-20 charecters long').len(4, 20);

    const errors = req.validationErrors();
    if(errors){
        return next(JSON.stringify(errors));
    }

    const firstName = req.body.firstname;
    const lastName  = req.body.lastname;
    const username  = req.body.username;
    const password  = req.body.password;


    bcrypt.hash(password, 10, function(err, hash) {
        if(err) {
            return next(err);
        }

        // Store hash in your password DB.
        db.query('INSERT INTO sec.user (first_name, last_name, username, password_hash) VALUES ($1, $2, $3, $4)',
            [firstName, lastName, username, hash], function(err, res){
                if(err) {
                    return next(err);
                }

                ress.send('Success');
        });
    });





});

// =====================================
// PROFILE SECTION =====================
// =====================================
// we will want this protected so you have to be logged in to visit
// we will use route middleware to verify this (the isLoggedIn function)
router.get('/profile', isLoggedIn, function(req, res) {
    // res.render('profile.ejs', {
    //     user : req.user // get the user out of session and pass to template
    // });

    res.send(200);
});

// =====================================
// LOGOUT ==============================
// =====================================
router.get('/logout', function(req, res) {
    req.logout();
    res.redirect('/');
});



// route middleware to make sure a user is logged in
{'fraction_non_alphanumeric': 0.22749648382559776, 'fraction_numerical': 0.007032348804500703, 'mean_word_length': 3.143481427530954, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, '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': '1298326', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1620, 'n_words': 438}