<commit_before>module.exports = function(app, passport) {

// normal routes ===============================================================

    var todo = require('./models/todo');

    // show the home page (will also have our login links)
    app.get('/', function(req, res) {
        res.render('index.ejs');
    });

    // PROFILE SECTION =========================
    app.get('/profile', isLoggedIn, function(req, res) {
        
        todo.find({user: req.user}, function(err, todos){
            res.render('profile.ejs', {
                user : req.user,
                todo : todos
            });
        }); 
        
    });
    /*
        Handler to add a new todo in the list
    */
    app.post('/add',isLoggedIn, function(req, res){
        var newTodo = new todo();
        newTodo.task = req.headers.task;
        newTodo.targetDate = req.headers.targetdate;
        newTodo.location = req.headers.location;
        newTodo.user  = req.user;
        newTodo.save(function(err){
            if(err)
                res.json(err);
            else
                res.send('success');
        });
    });
    /*
        Handler to delete a ToDo on request by the user
        TODO:
            Fix, vague delete. A case when a loggedin user can delete someone else's todo by knowing it's object
            id, which is not very difficult to get (simply the value of attribut 'value').
    */
    app.post('/delete',isLoggedIn, function(req, res){
        console.log(req.body.data);
        for(var i=0;i<req.body.data.length;i++){
            todo.find({_id:req.body.data[i]},function(err,todos){
                // console.log("User from todo : "+todos[0].user+"Type of : "+typeof todos[0].user);
                // console.log("User from Req : "+req.user._id+"Type of : "+typeof req.user._id);
                // if(todos[0].user===req.user._id)
                //     console.log("Authorize user !");
                if(err)
                    res.json(err);
            }).remove().exec();
        }
        res.redirect('success');
    });

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

// =============================================================================
// AUTHENTICATE (FIRST LOGIN) ==================================================
// =============================================================================

    // locally --------------------------------
        // LOGIN ===============================
        // show the login form
        app.get('/login', function(req, res) {
            res.render('login.ejs', { message: req.flash('loginMessage') });
        });<commit_msg>Now using moment.js for playing with date and time.<commit_after>module.exports = function(app, passport) {

// normal routes ===============================================================
    //access todo model
    var todo = require('./models/todo');

    //For displaying time 
    var moment = require('moment');

    // show the home page (will also have our login links)
    app.get('/', function(req, res) {
        res.render('index.ejs');
    });

    // PROFILE SECTION =========================
    app.get('/profile', isLoggedIn, function(req, res) {
        
        todo.find({user: req.user}, function(err, todos){
            todos.sort({targetDate:1})
            for(var i=0;i<todos.length;i++){
                todos[i].targetDate = moment(todos[i].targetDate).format("dddd, MMMM Do YYYY, h:mm:ss a");
            }
            res.render('profile.ejs', {
                user : req.user,
                todo : todos
            });
        }); 
        
    });
    /*
        Handler to add a new todo in the list
    */
    app.post('/add',isLoggedIn, function(req, res){
        var newTodo = new todo();
        newTodo.task = req.headers.task;
        newTodo.targetDate = new Date(Date.parse(req.headers.targetdate)).toISOString();
        // console.log("Date: "+newTodo.targetDate +" type :"+typeof(newTodo.targetDate));
        newTodo.location = req.headers.location;
        newTodo.user  = req.user;
        newTodo.save(function(err){
            if(err)
                res.json(err);
            else
                res.send('success');
        });
    });
    /*
        Handler to delete a ToDo on request by the user
        TODO:
            Fix, vague delete. A case when a loggedin user can delete someone else's todo by knowing it's object
            id, which is not very difficult to get (simply the value of attribut 'value').
    */
    app.post('/delete',isLoggedIn, function(req, res){
        console.log(req.body.data);
        for(var i=0;i<req.body.data.length;i++){
            todo.find({_id:req.body.data[i]},function(err,todos){
                // console.log("User from todo : "+todos[0].user+"Type of : "+typeof todos[0].user);
                // console.log("User from Req : "+req.user._id+"Type of : "+typeof req.user._id);
                // if(todos[0].user===req.user._id)
                //     console.log("Authorize user !");
                if(err)
                    res.json(err);
            }).remove().exec();
        }
        res.send('success');
    });

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

// =============================================================================
// AUTHENTICATE (FIRST LOGIN) ==================================================
// =============================================================================

    // locally --------------------------------
        // LOGIN ===============================
        // show the login form
        app.get('/login', function(req, res) {
            res.render('login.ejs', { message: req.flash('loginMessage') });
        });
{'fraction_non_alphanumeric': 0.2647207693605534, 'fraction_numerical': 0.0016871941960519656, 'mean_word_length': 2.035330261136713, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, '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': '5343440', 'n_tokens_mistral': 1649, 'n_tokens_neox': 1581, 'n_words': 348}