<commit_before>console.log('Loading restify server...');

var _ = require('lodash');
var Client = require('./mongo/client');
var userCollection = require('./mongo/userCollection');
var async = require('asyncawait/async');
var await = require('asyncawait/await');
var jwt = require('jsonwebtoken');

var client = new Client();
var db = null;
client.connect().then(function() {
    db = client.db;
    userCollection.db = db;
});

var restify = require('restify');
var api = restify.createServer({name: 'starting-strength'});
api.listen(process.env.PORT || 5000, function () {
    console.log('%s listening at %s', api.name, api.url)
});

// api.pre(restify.CORS({
//     origins: ['*', 'localhost', '127.0.0.1:5000', '127.0.0.1:5000', 'localhost:3000', 'localhost:8626'],
//     credentials: false,
//     headers: ['X-Requested-With', 
//                 'Access-Control-Request-Method', 
//                 'Access-Control-Request-Headers', 
//                 'Access-Control-Allow-Origin',
//                 'Authorization']
// }));

api.pre(restify.CORS());
restify.CORS.ALLOW_HEADERS.push('authorization');
api.pre(restify.fullResponse());
api.use(restify.bodyParser());

// function unknownMethodHandler(req, res) {
//   if (req.method.toLowerCase() === 'options') {
//       console.log('received an options method request');
//     var allowHeaders = ['Accept', 'Accept-Version', 'Content-Type', 'Api-Version', 'Origin', 'X-Requested-With']; // added Origin & X-Requested-With

//     if (res.methods.indexOf('OPTIONS') === -1) res.methods.push('OPTIONS');

//     res.header('Access-Control-Allow-Credentials', true);
//     res.header('Access-Control-Allow-Headers', allowHeaders.join(', '));
//     res.header('Access-Control-Allow-Methods', res.methods.join(', '));
//     res.header('Access-Control-Allow-Origin', req.headers.origin);

//     return res.send(204);
//   }
//   else
//     return res.send(new restify.MethodNotAllowedError());
// }

// api.on('MethodNotAllowed', unknownMethodHandler);

var secret = 'moocow';

var jwtRestify = require('restify-jwt');

api.use(jwtRestify({ secret: secret}).unless({path: [
                                                    '/ping', 
                                                    '/', 
                                                    '/login', 
                                                    '/register',
                                                    '/api/workouts/today']}));



api.get('/ping', function (req, res, next) {
    console.log("ping called");
    res.send(200, {response: true});
});

api.get('/', function(req, res) {
    console.log('default called');
    res.send(200);
});

api.post('/login', async (function(req, res)
{
    console.log("*** /login ***");
    try
    {
        var username = req.body.username;
        var password = <PASSWORD>;
        var foundUser = await (userCollection.findUser({username: username, password: password}));
        if(_.isObject(foundUser))
        {
            // TODO: encrypt passwords
            var profile = {
                username: username,
                email: foundUser.email,
                id: foundUser._id
            };
            var token = jwt.sign(profile, secret, { expiresInMinutes: 60*5 });
            res.json({result: true, token: token});
        }
        else
        {
            res.json({result: false, error: "Either your password isn't correct, or that username doesn't exist."});
        }
    }
    catch(error)
    {
        console.error("login error:", error);
    }
}));

api.post('/register', async (function(req, res)
{
    console.log("*** /login ***");
    var username = req.body.username;
    var password = <PASSWORD>;
    var email = req.body.email;
    var foundUser = await (userCollection.findUser({email: email}));
    if(_.isObject(foundUser))
    {
        res.json({result: false, error: "User already exists, please choose a different email address."})
    }
    else
    {
        var createdResult = await (userCollection.createUser(email, username, password));
        console.log("createdResult:", createdResult);
        if(createdResult)
        {
            res.json({result: true});
        }
        else
        {
            res.json({result: false, error: createdResult});
        }
        
    }
}));

api.get('/isloggedin', function(req, res) {
    console.log("*** /isloggedin ***");
    console.log(req.headers);
    res.send(200);<commit_msg>can decode and see token. HOUSTON, WE ARE A GOOO!!!!<commit_after>console.log('Loading restify server...');

var _ = require('lodash');
var Client = require('./mongo/client');
var userCollection = require('./mongo/userCollection');
var async = require('asyncawait/async');
var await = require('asyncawait/await');
var jwt = require('jsonwebtoken');

var client = new Client();
var db = null;
client.connect().then(function() {
    db = client.db;
    userCollection.db = db;
});

var restify = require('restify');
var api = restify.createServer({name: 'starting-strength'});
api.listen(process.env.PORT || 5000, function () {
    console.log('%s listening at %s', api.name, api.url)
});

// api.pre(restify.CORS({
//     origins: ['*', 'localhost', '127.0.0.1:5000', '127.0.0.1:5000', 'localhost:3000', 'localhost:8626'],
//     credentials: false,
//     headers: ['X-Requested-With', 
//                 'Access-Control-Request-Method', 
//                 'Access-Control-Request-Headers', 
//                 'Access-Control-Allow-Origin',
//                 'Authorization']
// }));

api.pre(restify.CORS());
restify.CORS.ALLOW_HEADERS.push('authorization');
api.pre(restify.fullResponse());
api.use(restify.bodyParser());

// function unknownMethodHandler(req, res) {
//   if (req.method.toLowerCase() === 'options') {
//       console.log('received an options method request');
//     var allowHeaders = ['Accept', 'Accept-Version', 'Content-Type', 'Api-Version', 'Origin', 'X-Requested-With']; // added Origin & X-Requested-With

//     if (res.methods.indexOf('OPTIONS') === -1) res.methods.push('OPTIONS');

//     res.header('Access-Control-Allow-Credentials', true);
//     res.header('Access-Control-Allow-Headers', allowHeaders.join(', '));
//     res.header('Access-Control-Allow-Methods', res.methods.join(', '));
//     res.header('Access-Control-Allow-Origin', req.headers.origin);

//     return res.send(204);
//   }
//   else
//     return res.send(new restify.MethodNotAllowedError());
// }

// api.on('MethodNotAllowed', unknownMethodHandler);

var secret = 'moocow';

var jwtRestify = require('restify-jwt');

api.use(jwtRestify({ secret: secret}).unless({path: [
                                                    '/ping', 
                                                    '/', 
                                                    '/login', 
                                                    '/register',
                                                    '/api/workouts/today']}));



api.get('/ping', function (req, res, next) {
    console.log("ping called");
    res.send(200, {response: true});
});

api.get('/', function(req, res) {
    console.log('default called');
    res.send(200);
});

api.post('/login', async (function(req, res)
{
    console.log("*** /login ***");
    try
    {
        var username = req.body.username;
        var password = req.body.password;
        var foundUser = await (userCollection.findUser({username: username, password: password}));
        if(_.isObject(foundUser))
        {
            // TODO: encrypt passwords
            var profile = {
                username: username,
                email: foundUser.email,
                id: foundUser._id
            };
            var token = jwt.sign(profile, secret, { expiresInMinutes: 60*5 });
            res.json({result: true, token: token});
        }
        else
        {
            res.json({result: false, error: "Either your password isn't correct, or that username doesn't exist."});
        }
    }
    catch(error)
    {
        console.error("login error:", error);
    }
}));

api.post('/register', async (function(req, res)
{
    console.log("*** /login ***");
    var username = req.body.username;
    var password = <PASSWORD>;
    var email = req.body.email;
    var foundUser = await (userCollection.findUser({email: email}));
    if(_.isObject(foundUser))
    {
        res.json({result: false, error: "User already exists, please choose a different email address."})
    }
    else
    {
        var createdResult = await (userCollection.createUser(email, username, password));
        console.log("createdResult:", createdResult);
        if(createdResult)
        {
            res.json({result: true});
        }
        else
        {
            res.json({result: false, error: createdResult});
        }
        
    }
}));

api.get('/isloggedin', function(req, res) {
    console.log("*** /isloggedin ***");
    console.log(req.headers);
    var token = req.headers.authorization.split(' ')[1];
    var decoded = jwt.decode(token);
    console.log("decoded:", decoded);
    res.send(200);
{'fraction_non_alphanumeric': 0.17372230752357973, 'fraction_numerical': 0.010638297872340425, 'mean_word_length': 2.255622991788647, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7341044', 'n_tokens_mistral': 2715, 'n_tokens_neox': 2639, 'n_words': 527}