// 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);
            }
        });
    }
};

// export module
module.exports = user;

@@ -2339,16 +2339,305 @@
 il: mail
+,%0A                token: jwt.sign(%7B%0A                    username: username%0A                %7D, global.config.authorize.token.secret, %7B%0A                    issuer: global.config.authorize.token.issuer,%0A                    expiresIn: global.config.authorize.token.expiresIn%0A                %7D)
 %0A       
@@ -2863,123 +2863,8 @@
 %7D);%0A
-        %7D, function(user, callback) %7B%0A            req.session.userId = user._id;%0A            callback(null, user);%0A
     
@@ -2948,20 +2948,65 @@
  error, 
-user
+%7B%0A                token: user.token%0A            %7D
 );%0A     

change signup