<commit_before>var models = require('../../../models')
    ,common = require('./common');

exports.referred_by_middleware = function(req,res,next)
{
    if('referred_by' in req.query)
    {
        req.session.referred_by = req.query['referred_by'];
        req.session.save(function(err,result)
        {
            next();
        });
    }
    else
        next();
};

exports.auth_middleware = function (req, res, next) {

//    if this request needs to be authenticated
    if(_.any(common.DONT_NEED_LOGIN_PAGES,function(dont)
    {
        return dont.test(req.path);
    }))
    {
        next();
        return;
    }

    if (req.isAuthenticated())
    {
        if(!req.session.user) {
                models.User.findById(req.session.auth.user._id || req.session.user_id ,function(err,user){
                    if(err){
                        console.log('couldn put user id' + err.message);
                        next();
                    }else{
                        if(user){
                            req.session.user_id = user._id;
                            req.session.avatar_url = user.avatar_url();

                            models.Notification.count({user_id: user._id}, function(err, count){
                                if(err){
                                    console.error('error finding user notifications');
                                    user.unseen_notifications = 0;
                                }
                                user.unseen_notifications = count;
                                req.session.user = user;
                                req.session.save(function(err)
                                {
                                    if(err)
                                        console.log('couldnt put user id' + err.message);
                                    next();
                                });
                            })
                        }
                        else{
                            require('./logout')(req,res);
                            next()
                        }
                    }<commit_msg>cvc<commit_after>var models = require('../../../models')
    ,common = require('./common');

exports.referred_by_middleware = function(req,res,next)
{
    if('referred_by' in req.query)
    {
        req.session.referred_by = req.query['referred_by'];
        req.session.save(function(err,result)
        {
            next();
        });
    }
    else
        next();
};

exports.auth_middleware = function (req, res, next) {

//    if this request needs to be authenticated
    if(_.any(common.DONT_NEED_LOGIN_PAGES,function(dont)
    {
        return dont.test(req.path);
    }))
    {
        next();
        return;
    }

    if (req.isAuthenticated())
    {
        if(!req.session.user) {
                models.User.findById(req.session.auth.user._id || req.session.user_id ,function(err,user){
                    if(err){
                        console.log('couldn put user id' + err.message);
                        next();
                    }else{
                        if(user){
                            req.session.user_id = user._id;
                            req.session.avatar_url = user.avatar_url();

                            models.Notification.count({user_id: user._id}, function(err, count){
                                if(err){
                                    console.error('error finding user notifications');
                                    user.unseen_notifications = 0;
                                }
                                user.unseen_notifications = count;
                                req.session.user = user;
                                req.session.save(function(err)
                                {
                                    if(err)
                                        console.log('couldnt put user id' + err.message);
                                    next();
                                });
                            })
                        }
                        else{
                            require('./logout')(req,res);
                        }
                    }
{'fraction_non_alphanumeric': 0.1080824840009481, 'fraction_numerical': 0.00047404598246029864, 'mean_word_length': 0.9340054995417049, 'pattern_counts': {'":': 0, '<': 3, '<?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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7541644', 'n_tokens_mistral': 1063, 'n_tokens_neox': 1005, 'n_words': 155}