Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about accessing user #116

Closed
davidtlee opened this issue Feb 12, 2015 · 2 comments
Closed

Question about accessing user #116

davidtlee opened this issue Feb 12, 2015 · 2 comments

Comments

@davidtlee
Copy link

I (initially accidentally) tried to access the user variable in one of my templates ({{user}}) and found that it worked despite not having set the user variable anywhere in my controllers. The object returned was structured as follows:

{"id":1,"provider":"google_oauth2","uid":"<some_uid>","name":"David Lee","nickname":null,"image":"<some_image>","email":"<email>","signedIn":true,"configName":"default"}

I thought that somehow ng-token-auth was automatically setting this variable for me which was awesome.

Later, I tried accessing that variable in one of my controllers (console.log(user)) and it said that the variable was undefined. Do you know what's going on? And is there a way to access that user variable on the controller side?

@harpster604
Copy link

Try using $auth.user or $rootScope.user in your controller. Make sure to inject $auth or $rootScope ointo your controller. These should allow access to the user object in the controller.

app.controller('MyCtrl', function ($scope, $auth, $rootScope) {
     console.log($rootScope.user);
     // OR
     console.log($auth.user);
});

But just keep in mind that the user object might be empty during the initial run if it's still in the process of being retrieved from the server. I've used an event to grab it once validation is successful in this case.

app.controller('MyCtrl', function ($scope, $auth, $rootScope) {
     $rootScope.$on('auth:validation-success', function(ev, user) {
          console.log($rootScope.user);
      });
});

@davidtlee
Copy link
Author

$rootScope.user worked! :) Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants