<commit_before>var User = require('../models/user.js');
var config = require('../../config.js');
var secretKey = config.secretKey;
var jsonwebtoken = require('jsonwebtoken');

function createToken(user) {
    
   var token = jsonwebtoken.sign({
            _id: user._id,
            first_name: user.first_name,
            last_name: user.last_name,
            username: user.username,
        },  secretKey, {
            expiresInMinute: 1440 
        });
    
    return token; 
}

module.exports = function (app, express) {


    var api = express.Router();
    
    //SIGNUP API

    api.post('/signup', function (req, res) {

        var user = new User({

            first_name: req.body.first_name,
            last_name: req.body.last_name,
            email: req.body.email,
            username: req.body.username,
            password: <PASSWORD>,
            phone_number: req.body.phone_number

        })

        user.save(function (err) {

            if (err) {
                res.send(err);
                return;
            }
            
            res.json({ message: 'User has benn created!'});

        });
    });
    
    // GET ALL USERS API
    
    api.get('/users', function(req, res) {
     
            User.find({}, function(err, users){
                if(err)  {
                    res.send(err); 
                    return; 
                }
                res.json(users); 
            });
    });
    
    // LOGIN API 
    
    api.post('/login', function(req, res){
        
        User.findOne({
            username: req.body.username 
            
        }).select('password').exec(function (err, user){
            
            if(err) throw err;
            
            if(!user) {
                res.send({ message: "User doesn't exist"});   
            } else if(user) {
                
                var validPassword = user.comparePassword(req.body.password);
                
                if(!validPassword) {
                    res.send({ message: "Invalid Password"});
                } else {
                    
                    //token 
                    var token = createToken(user); 
                    
                    res.json({
                        success: true, 
                        message: "Successful login!",
                        token: token
                    });    
                }
            }
        });
    });
    
    
    
    

    return api; 
}<commit_msg>starting to add middleware for token verification<commit_after>var User = require('../models/user.js');
var config = require('../../config.js');
var secretKey = config.secretKey;
var jsonwebtoken = require('jsonwebtoken');

function createToken(user) {
    
   var token = jsonwebtoken.sign({
            _id: user._id,
            first_name: user.first_name,
            last_name: user.last_name,
            username: user.username,
        },  secretKey, {
            expiresInMinute: 1440 
        });
    
    return token; 
}

module.exports = function (app, express) {


    var api = express.Router();
    
    //SIGNUP API

    api.post('/signup', function (req, res) {

        var user = new User({

            first_name: req.body.first_name,
            last_name: req.body.last_name,
            email: req.body.email,
            username: req.body.username,
            password: <PASSWORD>,
            phone_number: req.body.phone_number

        })

        user.save(function (err) {

            if (err) {
                res.send(err);
                return;
            }
            
            res.json({ message: 'User has benn created!'});

        });
    });
    
    // GET ALL USERS API
    
    api.get('/users', function(req, res) {
     
            User.find({}, function(err, users){
                if(err)  {
                    res.send(err); 
                    return; 
                }
                res.json(users); 
            });
    });
    
    // LOGIN API 
    
    api.post('/login', function(req, res){
        
        User.findOne({
            username: req.body.username 
            
        }).select('password').exec(function (err, user){
            
            if(err) throw err;
            
            if(!user) {
                res.send({ message: "User doesn't exist"});   
            } else if(user) {
                
                var validPassword = user.comparePassword(req.body.password);
                
                if(!validPassword) {
                    res.send({ message: "Invalid Password"});
                } else {
                    
                    //token 
                    var token = createToken(user); 
                    
                    res.json({
                        success: true, 
                        message: "Successful login!",
                        token: token
                    });    
                }
            }
        });
    });
    
    
    app.use(function(req,res, next){
        
        console.log("Somebody just came to our app!"); 
        
        var token = req.body.token || req.param('token') || req.headers['x-access-token'];
        
        // check if token exists
        if(token) {
            
            jsonwebtoken.verify(token, secretKey, function(err, decoded){
                
                if(err) {
                    res.status(403).send({ success: false, message: "Failed to authenticate user"});
                    
                } else {
                    
                    req.decoded = decoded; 
                 
                    next(); 
                }
            });
        } else {
            res.status(403).send({ sucess: false, message: "No Token Provided"});
        }
    });
    
    
    return api; 
    
}
{'fraction_non_alphanumeric': 0.11623108665749655, 'fraction_numerical': 0.002407152682255846, 'mean_word_length': 1.0605738575982997, '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': '1020823', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1478, 'n_words': 309}