<commit_before>var Hoek = require('hoek');
var Users = require('../users.json');
var Boom = require('boom');
var Joi = require('joi');


var internals = {};


internals.getValidateUser = function (username, password){

    return new Promise(function (resolve, reject){

        var account = null;

        // console.log(username);
        // console.log(password);

        account = Users[username];

        if (!account || account.password !== password) {
            throw { errorNum: 2, message: 'Invalid password or username' };
        }

        // Remove sensitive data from user cookie info

        var accountCopy = Hoek.clone(account);
        delete accountCopy.password;

        // promise resolved

        resolve(accountCopy);
    });
};


exports.register = function (server, options, next) {

    // Code inside the callback function of server.dependency will only be executed
    // after AuthCookie plugin has been registered. It's triggered by server.start,
    // and runs before actual starting of the server.  It's done because the call to
    // server.route upon registration with auth:'cookie' config would fail and make
    // the server crash if the basic strategy is not previously registered by Auth.
    server.dependency('AuthCookie', function (server, next){

        server.route({
            method: 'POST',
            path: '/login',
            config: {
                description: 'Returns the login page',
                auth: {
                 mode: 'try',
                 strategy: 'university'
                },
                plugins: {
                    'hapi-auth-cookie': {
                        redirectTo: false
                    }
                },
                validate: {
                    payload: { // payload for POST, query for GET
                        username: Joi.string().min(3).max(6),
                        password: Joi.string().min(3).max(7)
                    }
                },
                handler: function (request, reply) {


                    // setInterval(function() {

                    internals.getValidateUser(request.payload.username, request.payload.password)
                        .then( function (account) {

                            request.auth.session.set(account); // Success { id: 1, email: '<EMAIL>' }

                            return reply(account);

                        }).catch( function (e) {

                            return reply(Boom.unauthorized(e.message)); // "oh, no!"
                        });

                    // }, 4000);
                }
            }
        });


        server.route({
            method: 'POST',
            path: '/logout',
            config: {
                auth: {
                    strategy: 'university',
                    scope: ['user', 'admin']
                },
                handler: function (request, reply) {

                    // setTimeout( function () {

                    request.auth.session.clear();<commit_msg>Remove Promise logic from user validation code<commit_after>var Hoek = require('hoek');
var Users = require('../users.json');
var Boom = require('boom');
var Joi = require('joi');


var internals = {};


/* Promise method but node.js v0.10.0 d/n like this.
internals.getValidateUserPromise = function (username, password){

    return new Promise(function (resolve, reject){

        var account = null;

        // console.log(username);
        // console.log(password);

        account = Users[username];

        if (!account || account.password !== password) {
            throw { errorNum: 2, message: 'Invalid password or username' };
        }

        // Remove sensitive data from user cookie info

        var accountCopy = Hoek.clone(account);
        delete accountCopy.password;

        // promise resolved

        resolve(accountCopy);
    });
};
*/


internals.getValidateUser = function (username, password){

    var account = null;

    account = Users[username];

    if (!account || account.password !== password) {
        return { errorNum: 2, message: 'Invalid password or username' };
    }

    var accountCopy = Hoek.clone(account);
    delete accountCopy.password;

    // promise resolved

    return accountCopy;
};


exports.register = function (server, options, next) {

    // Code inside the callback function of server.dependency will only be executed
    // after AuthCookie plugin has been registered. It's triggered by server.start,
    // and runs before actual starting of the server.  It's done because the call to
    // server.route upon registration with auth:'cookie' config would fail and make
    // the server crash if the basic strategy is not previously registered by Auth.
    server.dependency('AuthCookie', function (server, next){

        server.route({
            method: 'POST',
            path: '/login',
            config: {
                description: 'Returns the login page',
                auth: {
                 mode: 'try',
                 strategy: 'university'
                },
                plugins: {
                    'hapi-auth-cookie': {
                        redirectTo: false
                    }
                },
                validate: {
                    payload: { // payload for POST, query for GET
                        username: Joi.string().min(3).max(6),
                        password: Joi.string().min(3).max(7)
                    }
                },
                handler: function (request, reply) {

                    //  Originally promise logic was here but node.js v0.10.0
                    //  throws errors so removed it from project.
                    //  This is a quick fix to solve travis issues.

                    var user = internals.getValidateUser(request.payload.username, request.payload.password);

                    if (user.message === 'Invalid password or username'){

                        return reply(Boom.unauthorized(user.message)); // "oh, no!"
                    }

                    request.auth.session.set(user); // Success { id: 1, email: '<EMAIL>' }

                    return reply(user);
                }
            }
        });


        server.route({
            method: 'POST',
            path: '/logout',
            config: {
                auth: {
                    strategy: 'university',
                    scope: ['user', 'admin']
                },
                handler: function (request, reply) {

                    // setTimeout( function () {

                    request.auth.session.clear();
{'fraction_non_alphanumeric': 0.1089975845410628, 'fraction_numerical': 0.0037741545893719805, 'mean_word_length': 1.4701715137956748, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5956382', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1520, 'n_words': 469}