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

User is logged in? #46

Closed
andreasseiler opened this issue Oct 11, 2014 · 8 comments
Closed

User is logged in? #46

andreasseiler opened this issue Oct 11, 2014 · 8 comments

Comments

@andreasseiler
Copy link

Hi there,

first of all thanks for your great work!

I have a question, whats the best way to check wether a user is logged in or not? Seems like the validateUser method doesn't work ...

Thanks,
Andi

@andreasseiler
Copy link
Author

It seems like validateUser() should broadcast the event auth:validation-error if no user is signed in?

If yes, it doesn't (at least for me) :)

@lynndylanhurley
Copy link
Owner

validateUser returns a promise that will resolve if the user's credentials are valid. Otherwise the promise will be rejected. This will happen initially when the page is loaded, and if any cached credentials are found (in cookies or localStorage), the credentials will be verified by the API (unless they have expired).

If the credentials pass verification, a signedIn attribute will be set to true on the user object that is available to the $rootScope. The easiest way to check for a user's auth status is as follows:

Examples:

From javascript (controllers, directives, etc):

if ($rootScope.user.signedIn) {
  alert("Hello " + $rootScope.user.name + "!");
}

Or from templates:

<div ng-show='user.signedIn'>
  Hello {{ user.name }}!
</div>

Note that the templates must have access to the $rootScope for the above example to work.


And you're right about the event not broadcasting in some cases. That is a bug - I'll push a fix ASAP.

@andreasseiler
Copy link
Author

Hi there,

thanks for the quick response.

Unfortunately, the user object is always empty {} ... no matter if I'm logged in or not...

@lynndylanhurley
Copy link
Owner

Can you post the code that you're using to check the user object?

@andreasseiler
Copy link
Author

.run(function($rootScope){ console.log($rootScope.user); })

@lynndylanhurley
Copy link
Owner

It won't be available immediately at the start of the run phase because the data must first be retrieved from the API. Try something like this:

.run(function($rootScope, $auth) {
  $auth.validateUser().then(function() {
    console.log($rootScope.user);
  });
});

@andreasseiler
Copy link
Author

You are right ... thanks!!!

@lynndylanhurley
Copy link
Owner

=)

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