<commit_before>// import third party library
var mongoose = require('mongoose');
var async = require('async');

// import model
var Users = require('../../db_modules/models/users');

// import helper
var ResponseHelper = require('../helper/ResponseHelper');
var MongoHelper = require('../helper/MongoHelper');
var ServerError = require('../ServerError');

var user = {
    rootPath: 'user',
    actions: {}
};

user.actions.login = {
    path: 'login',
    method: 'put',
    execute: function(req, res) {
        var username = req.body.username || '';
        var password = req.body.password || '';

        Users.findOne({
            username: username
        }, function(error, user) {
            if (error) {
                // mongo error
                ResponseHelper.buildResponse(res, error);
            } else if (user === null) {
                // user not exists
                ResponseHelper.buildResponse(res, ServerError.ERR_INVALID_USER);
            } else {
                if (user.password === password) {
                    req.session.userId = user._id;
                    // success
                    ResponseHelper.buildResponse(res, null, user);
                } else {
                    ResponseHelper.buildResponse(res, ServerError.ERR_INVALID_USER);
                }
            }
        });
    }
};

user.actions.signup = {
    path: 'signup',
    method: 'post',
    execute: function(req, res) {
        var param = req.body;
        var mail = param.email || '';
        var username = param.username || '';
        var password = param.password || '';

        if (mail.length === 0 || username.length === 0 || password.length === 0) {
            ResponseHelper.buildResponse(res, ServerError.NotEnoughParam);
            return;
        }

        async.waterfall([function(callback) {
            Users.findOne({
                username: username
            }, function(error, user) {
                if (error) {
                    callback(error);
                } else if (user) {
                    callback(ServerError.ERR_USER_IS_EXISTS);
                } else {
                    callback();
                }
            });
        }, function(callback) {
            var user = new Users({
                username: username,
                password: password,
                email: mail
            });

            user.save(function(error, user) {
                if (error) {
                    callback(error);
                } else {
                    callback(null, user);
                }
            });
        }, function(user, callback) {
            req.session.userId = user._id;
            callback(null, user);
        }], function(error, user) {
            ResponseHelper.buildResponse(res, error, user);
        });
    }
};

user.actions.getMe = {
    path: '',
    method: 'get',
    permissionValidators: ['validateLogin'],
    execute: function(req, res) {
        var _id = MongoHelper.parseObjectId(req.session.userId);
        Users.findOne({
            _id: _id
        }, function(error, user) {
            if (error) {
                ResponseHelper.buildResponse(res, error);
            } else if (!user) {
                ResponseHelper.buildResponse(res, ServerError.ERR_NOT_LOGGED_IN);
            } else {
                ResponseHelper.buildResponse(res, null, user);
            }
        });
    }
};<commit_msg>change signup<commit_after>// import third party library
var mongoose = require('mongoose');
var async = require('async');

// import model
var Users = require('../../db_modules/models/users');

// import helper
var ResponseHelper = require('../helper/ResponseHelper');
var MongoHelper = require('../helper/MongoHelper');
var ServerError = require('../ServerError');

var user = {
    rootPath: 'user',
    actions: {}
};

user.actions.login = {
    path: 'login',
    method: 'put',
    execute: function(req, res) {
        var username = req.body.username || '';
        var password = req.body.password || '';

        Users.findOne({
            username: username
        }, function(error, user) {
            if (error) {
                // mongo error
                ResponseHelper.buildResponse(res, error);
            } else if (user === null) {
                // user not exists
                ResponseHelper.buildResponse(res, ServerError.ERR_INVALID_USER);
            } else {
                if (user.password === password) {
                    req.session.userId = user._id;
                    // success
                    ResponseHelper.buildResponse(res, null, user);
                } else {
                    ResponseHelper.buildResponse(res, ServerError.ERR_INVALID_USER);
                }
            }
        });
    }
};

user.actions.signup = {
    path: 'signup',
    method: 'post',
    execute: function(req, res) {
        var param = req.body;
        var mail = param.email || '';
        var username = param.username || '';
        var password = param.password || '';

        if (mail.length === 0 || username.length === 0 || password.length === 0) {
            ResponseHelper.buildResponse(res, ServerError.NotEnoughParam);
            return;
        }

        async.waterfall([function(callback) {
            Users.findOne({
                username: username
            }, function(error, user) {
                if (error) {
                    callback(error);
                } else if (user) {
                    callback(ServerError.ERR_USER_IS_EXISTS);
                } else {
                    callback();
                }
            });
        }, function(callback) {
            var user = new Users({
                username: username,
                password: password,
                email: mail,
                token: jwt.sign({
                    username: username
                }, global.config.authorize.token.secret, {
                    issuer: global.config.authorize.token.issuer,
                    expiresIn: global.config.authorize.token.expiresIn
                })
            });

            user.save(function(error, user) {
                if (error) {
                    callback(error);
                } else {
                    callback(null, user);
                }
            });
        }], function(error, user) {
            ResponseHelper.buildResponse(res, error, {
                token: user.token
            });
        });
    }
};

user.actions.getMe = {
    path: '',
    method: 'get',
    permissionValidators: ['validateLogin'],
    execute: function(req, res) {
        var _id = MongoHelper.parseObjectId(req.session.userId);
        Users.findOne({
            _id: _id
        }, function(error, user) {
            if (error) {
                ResponseHelper.buildResponse(res, error);
            } else if (!user) {
                ResponseHelper.buildResponse(res, ServerError.ERR_NOT_LOGGED_IN);
            } else {
                ResponseHelper.buildResponse(res, null, user);
            }
        });
    }
};
{'fraction_non_alphanumeric': 0.12480587321756317, 'fraction_numerical': 0.0008470986869970351, 'mean_word_length': 1.464, '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': '7543069', 'n_tokens_mistral': 1859, 'n_tokens_neox': 1780, 'n_words': 373}