'use strict';

angular.module('BaubleApp')
  .controller('MainCtrl', ['$scope', '$location', 'User', 'Alert',
    function ($scope, $location, User, Alert) {

        $scope.user = User.local();
        $scope.Alert = Alert;

        // if(!$scope.user) {
        //     $location.path('/login');
        // } else if(!$scope.user.organization_id) {
        //     console.log('new org');
        //     $location.path('/organization/new');
        // }

        $scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
            // controls the user menu
            $scope.user = User.local();
            if(!$scope.user) {
                $location.path('/login');
            } else if(!$scope.user.organization_id) {
                $location.path('/organization/new');
            }
        });

        // if(!$scope.user.organization) {
        //     $location.path($scope.user ? '/dashboard' : '/login');
        // }
        // $location.path($scope.user ? '/dashboard' : '/login');
        // if(!Auth.isLoggedIn()) {
        //     $location.path("/login");
        // } else {
        //     $location.path("/search");
        // }


    }]);

@@ -223,237 +223,8 @@
 t;%0A%0A
-        // if(!$scope.user) %7B%0A        //     $location.path('/login');%0A        // %7D else if(!$scope.user.organization_id) %7B%0A        //     console.log('new org');%0A        //     $location.path('/organization/new');%0A        // %7D%0A%0A
     
@@ -450,144 +450,95 @@
     
-$location.path('/login');%0A            %7D else if(!$scope.user.organization_id) %7B%0A                $location.path('/organization/new');%0A   
+var path = _.contains(%5B'main.forgot-password', 'main.reset-password'%5D, toState.name) ?%0A
     
@@ -542,26 +542,24 @@
         
-%7D%0A
         
 %7D);%0A%0A   
@@ -554,55 +554,34 @@
     
-%7D);%0A%0A        // if(!$scope.user.organization) %7B
+   toState.url : '/login';
 %0A   
@@ -577,34 +577,35 @@
 login';%0A        
-//
+   
      $location.p
@@ -612,159 +612,65 @@
 ath(
-$scope.user ? '/dashboard' : '/login');%0A        // %7D%0A        // $location.path($scope.user ? '/dashboard' : '/login');%0A        // if(!Auth.isLoggedIn()
+path);%0A            %7D else if(!$scope.user.organization_id
 ) %7B%0A
@@ -669,34 +669,35 @@
 n_id) %7B%0A        
-//
+   
      $location.p
@@ -704,38 +704,29 @@
 ath(
-%22/login%22);%0A        // %7D else %7B
+'/organization/new');
 %0A   
@@ -734,41 +734,13 @@
     
-//
     
- $location.path(%22/search%22);
+%7D
 %0A   
@@ -748,14 +748,11 @@
     
-// %7D%0A%0A
+%7D);
 %0A   

allow routes that don't require login