So the order is as follows

> someone goes to /login at which point loginController is loaded...

'use strict';

mintApp.controller('LoginController', ["$scope", "$rootScope", "$http", 
"$location", "AuthFactory", function ($scope, $rootScope, $http, $location, 
AuthFactory) {

    $scope.errors = [];
    $scope.user   = {};

    // Initialize form data to defaults
    $scope.loginData = {
        username : "Username",
        password : "Password"
    };

    $scope.displayErrors = 0;

    $scope.login = function () {
        AuthFactory.login($scope.loginData).then(function (phpData) {

            console.log(phpData);
            if (phpData.status === 'Success') {
                $scope.user.loggedIn = AuthFactory.user.isLoggedIn;
                $location.path('/client/' + phpData.user_id +'/account');
            }
            else {
                $scope.errors.splice(0, $scope.errors.length);
                $scope.displayErrors = 1;
                for (var i = 0; i < phpData.length; i++)
                    $scope.errors.push(phpData[i]);
            }

            return false;
        });

        return false;
    };

}]);

Then someone can submit the form loaded in the login view. When they submit 
the form, the login function in the loginController above makes a call to 
AuthFactory which is below...

'use strict';

mintApp.factory("AuthFactory", ["$http", "$rootScope", "$timeout", 
function($http, $rootScope, $timeout) {
    var factory = {};

    factory.user = {
        isLoggedIn : false
    };

    factory.login = function (loginData) {
        return $http.post('../MINT/scripts/php/validate_user', 
loginData).then(function (results) {
                // results is an object with the following attributes etc
                //      config object   - Config object with headers  
                //      data object     - This is what I have passed back 
from my script (array/ object)
                //      status          - an HTTP status code (i.e. 200, 
400 etc)
                var phpData = results.data;

                // Set factory.user.isLoggedIn, only if what returned an 
object and
                // not an array
                if (!angular.isArray(phpData)) {
                    if (phpData.account_type === 'client') {
                        // $timeout(function() {
                            broadcast('clientAuthenticated', phpData);
                        // }, 1000);
                        return phpData;
                    }
                    else {
                        console.log('Provider Section');
                        $timeout(function() {
                            broadcast('providerAuthenticated', phpData);
                        }, 1000);
                        return phpData;
                    }
                }
        });
    };

    var broadcast = function(event, data) {
        factory.user.isLoggedIn = true;
        $rootScope.$broadcast(event, data);
    };

    return factory;
}]);

which the hits the backend and if we get back an array we have errors.  If 
it's not an array we send a broadcast saying the client is authenticated 
then we go back to loginController where we check if we got back "Sucess" 
before I redirect the user to his account page /client/<userid>/account 
where I will display all the users info.

here is my routing for these pages...

'use strict';

var mintApp = angular.module('mintApp', ['ngRoute', 'bfLink', 'bfInput']);

mintApp.config(function ($routeProvider) {
    $routeProvider
    .when('/', {
        templateUrl: '../MINT/views/main.php'
    })
    .when('/login', {
        templateUrl: '../MINT/views/login.php',
        controller:  'LoginController'
    })
    .when('/client/signup', {
        templateUrl: '../MINT/views/client-membership.php',
        controller:  'ClientController'
    })
    .when('/provider/signup', {
        templateUrl: '../MINT/views/service-provider-membership.php',
        controller:  'ProviderController'
    })
    .when('/registered', {
        templateUrl: '../MINT/views/registered.php',
        controller:  'MainController'
    })
    .when('/:email/activate/:activationId', {
        templateUrl: '../MINT/views/activate.php',
        controller: 'ActivateController'
    })
    .when('/client/:user_id/account', {
        templateUrl: '../MINT/views/account.php',
        controller: 'ClientController',
        restrict: true
    })
    .when('/user/:userId/edit', {
        templateUrl: '../MINT/views/main.php',
        controller: 'MainController',
        restrict: true
    })
    .otherwise({redirectTo: '/'});
});

mintApp.run(['$rootScope', '$log', '$location', 'AuthFactory', 
function($rootScope, $log, $location, AuthFactory) {
    $rootScope.$log = $log;

    // Protecting member pages
    $rootScope.$on("$routeChangeStart", function (event, next, current) {
        if (next && next.$$route && next.$$route.restrict) {
            console.log("We hit a protected route");
            console.log("AuthFactory.user.isLoggedIn = " + 
AuthFactory.user.isLoggedIn);
            if (!AuthFactory.user.isLoggedIn)
                $location.path('/');
        }
    });
}]);

Let me know if I am missing anything please so I can provide it for you

On Sunday, April 20, 2014 9:29:49 PM UTC-4, Raul Vieira wrote:
>
> When does the auth happen relative to the client controller being 
> constructed?
>
> On Apr 20, 2014, at 9:24 PM, Billy Figueroa <[email protected]<javascript:>> 
> wrote:
>
> Ok so this issue has not been resolved. I am trying to avoid putting the 
> logic in my main controller. I did notice this has to be one of the "time" 
> issues you mentioned luke.
>
> I m guessing the MainController receives the broadcast because it is some 
> how already loaded and ready to go, but my client controller somehow is not?
>
> I tried one thing and noticed it is definately a timing issue. I put a 
> $timeout call for 1 second and I noticed that if I do that it hits the 
> broadcast. How can I deal with these timing issues? I tried to change my 
> logic from using jquery success callbacks to using the promise then instead
>
> ANYONE who can help can answer this, not just Luke. I addressed him 
> because hes been helping. Thanks Luke!
>
> On Thursday, April 17, 2014 9:53:56 PM UTC-4, Billy Figueroa wrote:
>>
>> Hi All,
>>
>> so I have a quick issue with my controller ordering I guess.
>>
>> I have the following shell page layout...
>>
>> <html>
>>    <head></head>
>>    <body ng-controller="MainController">
>>       <div ng-view></div>
>>    </body>
>> </html>
>>
>> I have an ng-view template that the routeProvider is loading 
>> (account.php) when we hit the url and load ClientController...
>>
>>     .when('/client/:user_id/account', {
>>         templateUrl: '../MINT/views/account.php',
>>         controller: 'ClientController',
>>         restrict: true
>>     })
>>
>> I also have an AuthFacotory factory where, once a user logs in and I get 
>> data back from my backend (PHP) I broadcast a signal to say we are 
>> authenticated
>>
>>                     if (phpData.account_type === 'client')
>>                         $rootScope.$broadcast('clientAuthenticated', 
>> phpData);
>>                     else
>>                         $rootScope.$broadcast('providerAuthenticated', 
>> phpData);
>>
>> I have the following I guess "watches" both inside the MainController and 
>> ClientController
>>
>>
>> <-- MainController -->
>>     $scope.$on('clientAuthenticated', function(phpData) {
>>         console.log("We caught the broadcast for 
>> clientAuthenticated[MainController]");
>>     });
>>
>> <-- ClientController -->
>>     $scope.$on('clientAuthenticated', function(phpData) {
>>         console.log("We caught the broadcast for 
>> clientAuthenticated[ClientController]");
>>     });
>>
>>
>> I am only "catching" the one in the MainController.
>>
>> Based on the structure being that the ClientController is loaded as part 
>> of the view, I m guessing its some sort of a child of the MainController 
>> but why is it not "catching" that broadcast?
>>
>> the weird part is that I have other code in the ClientController and that 
>> gets executed but the console.log inside the scope statement is not
>>
>> I wanted to create a jsfiddle or plunker but I rarely get them working 
>> when I try to do it in a non global modular way like real apps are written 
>> i.e. var myApp = angular.module('myApp', [])
>>
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] <javascript:>.
> To post to this group, send email to [email protected] <javascript:>
> .
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to