'use strict';

/*
 * Pretty simple controller for the login screen.
 * Not much else to say about this one!
 */

angular.module('login', ['peerhandler'])

.controller('LoginCtrl', function($scope, $state, $localStorage, $ionicHistory, $http, peerFactory, socketFactory, configFactory) {
        // Disable back button so we can't back to login!
        $ionicHistory.nextViewOptions({
            disableBack: true
        });

        var url = configFactory.getValue('apiUrl');

        // Hack so we're disconnected for sure!
        peerFactory.disconnectFromPeerJS();

        if ($localStorage.user) {
            $scope.user = $localStorage.user;
        }

        $scope.goToSignUp = function () {
            $state.go('verify');
        }

        $scope.login = function(user) {
            if (typeof user !== 'undefined' && user.phoneNumber && user.password) {

                $http.post(
                    url + 'auth/login',
                    { phoneNumber : user.phoneNumber,
                     password     : user.password }
                ).then(function success(results) {

                    $localStorage.user  = results.data.user;
                    $localStorage.token = results.data.user.token;
 
                    peerFactory.connectToPeerJS(user.phoneNumber).then(function() {
                        return socketFactory.connectToServer($localStorage.token);
                    }).then(function () {
                        $state.go('tabs.contacts');
                    })
                    .catch(function (error) {
                        peerFactory.disconnectFromPeerJS();
                        socketFactory.disconnectFromServer();
                    });

                }, function error(err) {
                    console.log(err);
                })
            }
        };
});


@@ -228,21 +228,26 @@
 istory, 
-$http
+apiFactory
 , peerFa
@@ -430,61 +430,8 @@
 );%0A%0A
-        var url = configFactory.getValue('apiUrl');%0A%0A
     
@@ -816,25 +816,24 @@
 password) %7B%0A
-%0A
             
@@ -840,149 +840,47 @@
     
-$http.post(%0A                    url + '
+apiFactory.
 auth
-/
+.
 login
-',%0A                    %7B phoneNumber : user.phoneNumber,%0A                     password     :
+(user.phoneNumber,
  use
@@ -889,18 +889,17 @@
 password
- %7D
+)
 %0A       
@@ -907,17 +907,20 @@
         
-)
+    
 .then(fu
@@ -945,17 +945,66 @@
 ults) %7B%0A
-%0A
+                        console.log(results);%0A    
         
@@ -1049,19 +1049,18 @@
 lts.
-data.
 user;%0A
+    
     
@@ -1109,13 +1109,8 @@
 lts.
-data.
 user
@@ -1118,17 +1118,25 @@
 token;%0A 
-%0A
+    %0A    
         
@@ -1235,16 +1235,20 @@
         
+   
+ 
 return s
@@ -1318,16 +1318,20 @@
         
+    
 %7D).then(
@@ -1360,32 +1360,36 @@
                 
+  
+  
 $state.go('tabs.
@@ -1396,24 +1396,52 @@
 contacts');%0A
+                        %7D)%0A%0A
             
@@ -1493,24 +1493,68 @@
 n (error) %7B%0A
+                        console.log(error);%0A
             
@@ -1690,107 +1690,8 @@
  %7D);
-%0A%0A                %7D, function error(err) %7B%0A                    console.log(err);%0A                %7D)
 %0A   

Update login controller to ues new API module