Not seeing your app controller, I'd agree with GP - it's probably being
instantiated before the actual promise for the user resolves.

a few things:

1) the $http.get method returns a promise, so that whole $q.defer() pattern
you do in the .service block is unnecessary. You could just as easily
reduce that to this.get = function () {return $http.get('url');} - then
CurrentUser.get() would have a .then and .catch resolution just as you need.

1a) Don't forget to put that .catch on your promise - if you don't have an
error condition somewhere in that promise chain, you'll miss errors.

2) Putting that "log the user in" code in your run block is probably not a
great idea anyway. If your pages respond to $scope.authenticated properly,
then when the promise *does* resolve, the rendering will shift from the
"you're not logged in" to the "you're logged in!" mode either way.

3) If you want to prevent the user from transitioning into a state before
they are logged in, you need to either (a) put it into a resolve block as
GP suggests in the routing setup, or (b) do a
CurrentUser.get().then(whatever) in the controller for that route, and
manually transition the user somewhere else when the login process fails.

Personally, I have a little "click here to log in!" div that initiates the
oauth2 dance with my backend that only displays when $rootScope.apiToken is
not defined - when that happens, the div appears, but use of some
modal-style overlays makes it lay on top of all the rest of the rendering,
no matter where the user is in the app. It doesn't display anything
interesting underneath the div anyway, but this way I can get the user
logged in and everything loading properly without needing to worry about
redirecting to an actual /login route.

e


On Sun, Jun 22, 2014 at 8:55 AM, gp <[email protected]> wrote:

> As it is promise, you need to move code to route.resolve.  It will wait
> for promise to resolve.  'run' will not wait, I think, that's why you are
> getting null.
>
> Best luck.
>
>
> On Saturday, June 21, 2014 4:28:40 PM UTC-5, Jason Girdner wrote:
>>
>> Ok I am beating my head into the wall trying to figure out why my service
>> is returning a undefined value on page load.  Below is my service and the
>> run function I have.  I can see from my console log that I am getting the
>> correct value.  But when I log ($rootScope.user) from my app controller I
>> get undefined.  I can see in the Angular inspector that the data I am
>> trying to get in the controller is in the rootScope but for some reason its
>> loading undefined before it has a chance to fetch the data.  I have played
>> around for hours with the promise function and can't seem to get it to
>> work.  Any help will be appreciated.
>>
>> .service('CurrentUser', function($http, $q) {
>>   this.get = function() {
>>     var deferred = $q.defer();
>>     var url = '/api/current-user?format=json';
>>     $http.get(url).success(function(data, status) {
>>       deferred.resolve(data);
>>     }).error(function(data, status) {
>>       deferred.reject(data);
>>     });
>>     return deferred.promise;
>>   }
>> })
>> .run(function (CurrentUser, $rootScope){
>>   var promise = CurrentUser.get();
>>   promise.then(function(data) {
>>     if (data.is_active == false) {
>>       $rootScope.authenticated = false
>>       $rootScope.user = null
>>       console.log($rootScope.authenticated)
>>     } else {
>>       $rootScope.authenticated = true
>>       $rootScope.user = data;
>>       console.log($rootScope.authenticated)
>>     }
>>   });
>> })
>>
>  --
> 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.
>

-- 
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