'use strict';
/*jslint nomen: true unparam: true*/

var
    ResetPasswordToken = require('mongoose').model('ResetPasswordToken'),
    User = require('mongoose').model('User');

exports.create = function (req, res, next) {
    ResetPasswordToken.createByUser(req.body.user, function(error, token) {
        if (error) next(error);
        res.send({error: error, token: token});
    });
};

exports.reset = function (req, res, next) {
    ResetPasswordToken.findOne({_id: req.body.token}, function (findTokenError, token) {
        if (findTokenError || !token) {
            next(findTokenError);
            res.send({error: 'TOKEN_NOT_FOUND'});
        } else {
            User.findOne({_id: token.user}, function(findUserError, user) {
                if (findUserError || !user) {
                    next(findUserError);
                    res.send({error: 'USER_NOT_FOUND'});
                } else {
                    user.setPassword(req.body.password, function(setPasswordError) {
                        if (setPasswordError) {
                            next(setPasswordError);
                            res.send({error: 'SET_PASSWORD_ERROR'});
                        } else {
                            res.send({});
                        }
                    });
                }
            });
        }
    });
};

@@ -207,32 +207,251 @@
 q, res, next) %7B%0A
+    User.findOne(%7Bemail: req.body.email%7D, function(findUserError, user) %7B%0A        if (findUserError %7C%7C !user) %7B%0A            next(findUserError);%0A            res.send(%7Berror: 'USER_NOT_FOUND'%7D);%0A        %7D else %7B%0A        
     ResetPasswor
@@ -474,21 +474,16 @@
 ser(
-req.body.
 user
+._id
 , fu
@@ -497,32 +497,40 @@
 error, token) %7B%0A
+        
         if (erro
@@ -541,24 +541,32 @@
 ext(error);%0A
+        
         res.
@@ -597,24 +597,50 @@
 n: token%7D);%0A
+            %7D);%0A        %7D%0A
     %7D);%0A%7D;%0A%0A

create reset password token by user email