<commit_before>var util = {}
var Room = require('./models').Room

var async = require('async')
var jwt = require('jsonwebtoken')

//Helper function that sends JSON responses
function handleResp(res, status, data) {
    if (!(typeof data == 'object')) {
        data = {error: data}
    }
    res.status(status)
    res.json(data)
    res.end()
}

//Function used for validating request when a room is accessed
util.validateRoom = function (req, res, next) {
    var room_path = null
    //Pulls room_path identifier from body, path, or query string
    if (req.body.room_path) {
        room_path = req.body.room_path
    } else if (req.params.room_path) {
        room_path = req.params.room_path
    } else {
        room_path = req.query.room_path
    }

    Room.findOne({path: room_path}, function (err, room) {
        if (err) {
            return next(err)
        } else if (!room) {
            return handleResp(res, 404, 'Room not found')
        } else if (req.user && req.user._id != room.owner.toString()) {
            return handleResp(res, 406, 'User does not own room') //Matches req.user against the owner of the Room
        } else {
            req.room = room
            next()
        }
    })
}

//Verifies the presence of an authenticated user
util.validateUser = function(req, res, next) {
    if (!req.user) {
        return handleResp(res, 401, 'User not authenticated')
    } else {
        next()
    }
}

//Helper function used to verify the presence of certain fields in the request body
util.validateBody = function(fields) {
    return function(req, res, next) {
        async.each(fields, function(field, done) {
            if (!Object.prototype.hasOwnProperty.call(req.body, field)) { //If field is not found in request body
                handleResp(res, 400, {error: 'Invalid fields'})
                return done('Invalid Fields')
            } else {
                done()
            }
        }, function(err) {
            if (!err) { //Passes request onto next route middleware if all fields are present
                next()
            }
        })
    }
}

//Helper function used for generating jsonwebtoken containing a user instance
util.generateToken = function(user) {
    return jwt.sign(user.toObject(), process.env.SECRET || 'dev_secret')
}

util.handleResp = handleResp

module.exports = util<commit_msg>Made validateUser helper function redirect to a view rather than return a JSON object<commit_after>var util = {}
var Room = require('./models').Room

var async = require('async')
var jwt = require('jsonwebtoken')

//Helper function that sends JSON responses
function handleResp(res, status, data) {
    if (!(typeof data == 'object')) {
        data = {error: data}
    }
    res.status(status)
    res.json(data)
    res.end()
}

//Function used for validating request when a room is accessed
util.validateRoom = function (req, res, next) {
    var room_path = null
    //Pulls room_path identifier from body, path, or query string
    if (req.body.room_path) {
        room_path = req.body.room_path
    } else if (req.params.room_path) {
        room_path = req.params.room_path
    } else {
        room_path = req.query.room_path
    }

    Room.findOne({path: room_path}, function (err, room) {
        if (err) {
            return next(err)
        } else if (!room) {
            return handleResp(res, 404, 'Room not found')
        } else if (req.user && req.user._id != room.owner.toString()) {
            return handleResp(res, 406, 'User does not own room') //Matches req.user against the owner of the Room
        } else {
            req.room = room
            next()
        }
    })
}

//Verifies the presence of an authenticated user
util.validateUser = function(req, res, next) {
    if (!req.user) {
        res.redirect('/')
    } else {
        next()
    }
}

//Helper function used to verify the presence of certain fields in the request body
util.validateBody = function(fields) {
    return function(req, res, next) {
        async.each(fields, function(field, done) {
            if (!Object.prototype.hasOwnProperty.call(req.body, field)) { //If field is not found in request body
                handleResp(res, 400, {error: 'Invalid fields'})
                return done('Invalid Fields')
            } else {
                done()
            }
        }, function(err) {
            if (!err) { //Passes request onto next route middleware if all fields are present
                next()
            }
        })
    }
}

//Helper function used for generating jsonwebtoken containing a user instance
util.generateToken = function(user) {
    return jwt.sign(user.toObject(), process.env.SECRET || 'dev_secret')
}

util.handleResp = handleResp

module.exports = util
{'fraction_non_alphanumeric': 0.1141418380193034, 'fraction_numerical': 0.0044062106588334034, 'mean_word_length': 2.3035343035343034, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4271215', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1357, 'n_words': 433}