googlearchive / generator-angularfire

Yeoman generator for AngularFire: Angular + Firebase apps
190 stars 52 forks source link

ng-show-auth / ng-hide-auth do not work on views? #43

Closed yipcma closed 9 years ago

yipcma commented 9 years ago

Hi all, this is probably more of a question than issues about the code:

Could anyone give me some hints as to how to use the ng-show-auth in views? i see that it works fine on index.html, but in my other html it simply hides the element (always).

maxbortone commented 9 years ago

You just insert the directive in the element you want to hide or show like this:

<a href="/account" ng-show-auth="">account</a>

The code for the directive adds a listener for the user object in the simpleLogin factory, which is updated on authentication changes.

I haven't had any problems with those directives so far. Can you give more details?

yipcma commented 9 years ago

a reproducible example: if i add to the form element on views/chat.thml:

<form ng-show-auth="">
 <input placeholder="Message..." ng-model="newMessage">
 <button type="submit" ng-click="addMessage(newMessage);newMessage = null;">send</button>
</form>

either when i am logged in or not, this form is not shown. Would you help me out a bit on this please? Thank you very much.

maxbortone commented 9 years ago

Yep, you are right. It seems to be an issue with the scaffolded app: the ng-hide-authand ng-show-auth directives don't work inside views. The problem seems to be in the simpleLogin factory. I've changed a couple of things in the watch method and it seems to be working. Here is what you need to change:

from this

watch: function(cb, $scope) {
    listeners.push(cb);
    auth.$waitForAuth(cb);
    .....
};

to this:

watch: function(cb, $scope) {
    auth.$waitForAuth().then(function(user) {
         cb(user);
    };
    listeners.push(cb);
    .....
};

Basically the $waitForAuth() method returns a promise, fullfilled with the user authentication status and other authentication data, which is then passed to the functions in the listeners array. This should be fixed in the generator!

yipcma commented 9 years ago

thanks so much. Now it works with this

watch: function(cb, $scope) {
    auth.$waitForAuth().then(function(user) {
         cb(user);
    });
    listeners.push(cb);
    .....
};

may the generator be fixed soon.

katowulf commented 9 years ago

Fixed. Thanks @maxbortone!

(btw, that can be simplified to auth.$waitForAuth().then(cb);)