var async = require( "async" );
var authUtils = require( "api-utils" ).authentication;
var User = require( "./models" ).User;


exports.register = function ( req, res, next ) {

    req.checkBody( "email", "Not a valid email address" ).isEmail();
    req.checkBody( "password", "Password must be at least 6 characters" ).isLength( 6 );

    var validationErrors = req.validationErrors();

    if ( validationErrors ) {
        return res.status( 400 ).json( validationErrors );
    }

    var user = {
        email: req.body.email,
        password: req.body.password
    };

    User.register( user, function ( err, newUser ) {

        if ( err ) {
            return next( err );
        }

        return res.status( 201 ).json( {
            token: authUtils.generateJWT( newUser, [ "_id", "email" ] )
        } );

    } );

};


exports.authenticate = function ( req, res, next ) {

    req.checkBody( "email", "Not a valid email address" ).isEmail();
    req.checkBody( "password", "Password is required" ).isLength( 1 );

    var validationErrors = req.validationErrors();

    if ( validationErrors ) {
        return res.status( 400 ).json( validationErrors );
    }

    var user = {
        email: req.body.email,
        password: req.body.password
    };

    User.authenticate( user, function ( err, existingUser ) {

        if ( err ) {
            return next( err );
        }

        return res.status( 200 ).json( {
            token: authUtils.generateJWT( existingUser, [ "_id", "email" ] )
        } );

    } );

};


exports.getDetail = function ( req, res, next ) {

    User.detailById( req.user._id, function ( err, detail ) {

        if ( err ) {
            return next( err );
        }

        return res.status( 200 ).json( detail );

    } );

};


exports.unRegister = function ( req, res, next ) {

    User.unRegister( req.user, function ( err ) {

        if ( err ) {
            return next( err );
        }

        return res.status( 204 ).json();

    } );

};


exports.generateKey = function ( req, res, next ) {

    User.generateKeyById( req.user._id, function ( err, key ) {

        if ( err ) {
            return next( err );
        }

        return res.status( 201 ).json( {
            key: key
        } );

    } );

};

@@ -125,35 +125,275 @@
 ;%0A%0A%0A
-exports.register = function
+var accountCreateQueue = process.env.BASE_CREATE_QUEUE;%0Aif ( !accountCreateQueue ) %7B%0A    console.log( %22BASE_CREATE_QUEUE environment variable is required.%22 );%0A    process.exit( 1 );%0A%7D%0A%0A%0Aexports.register = function ( messageBroker ) %7B%0A%0A    function registrationHandler
  ( r
@@ -403,32 +403,36 @@
  res, next ) %7B%0A%0A
+    
     req.checkBod
@@ -480,32 +480,36 @@
 .isEmail();%0A    
+   
+ 
 req.checkBody( %22
@@ -574,24 +574,28 @@
 ngth( 6 );%0A%0A
+    
     var vali
@@ -630,32 +630,36 @@
 nErrors();%0A%0A    
+  
+  
 if ( validationE
@@ -660,32 +660,36 @@
 ationErrors ) %7B%0A
+    
         return r
@@ -727,35 +727,43 @@
 onErrors );%0A    
-%7D%0A%0A
+    %7D%0A%0A    
     var user = %7B
@@ -763,32 +763,36 @@
 ser = %7B%0A        
+  
+  
 email: req.body.
@@ -790,32 +790,36 @@
 req.body.email,%0A
+    
         password
@@ -834,36 +834,90 @@
 dy.password%0A    
+  
+  
 %7D;%0A%0A
+        function registerUser ( done ) %7B%0A%0A        
     User.registe
@@ -954,32 +954,40 @@
 er ) %7B%0A%0A        
+    
+    
 if ( err ) %7B%0A   
@@ -975,32 +975,573 @@
    if ( err ) %7B%0A
+                    return done( err );%0A                %7D%0A%0A                return done( null, newUser );%0A%0A            %7D );%0A%0A        %7D%0A%0A        function sendRabbitMessage ( newUser, done ) %7B%0A%0A            var queue = accountCreateQueue + %22.%22 + newUser.subscription.serverName;%0A            var message = %7B%0A                user_key: newUser._id%0A            %7D;%0A            messageBroker.publish( queue, message );%0A            return done( null, newUser );%0A        %7D%0A%0A        function sendResponse ( err, newUser ) %7B%0A%0A            if ( err ) %7B%0A    
             retu
@@ -1556,35 +1556,43 @@
  err );%0A        
-%7D%0A%0A
+    %7D%0A%0A    
         return r
@@ -1620,32 +1620,36 @@
 ( %7B%0A            
+ 
+   
 token: authUtils
@@ -1696,37 +1696,160 @@
 %5D )%0A        
+    
 %7D );%0A%0A
+    
     %7D
- )
+%0A%0A        async.waterfall( %5B registerUser, sendRabbitMessage %5D, sendResponse );%0A    %7D%0A%0A    return registrationHandler
 ;%0A%0A%7D;%0A%0A%0Aexpo

Update registration handler