FirebaseExtended / angularfire

AngularJS bindings for Firebase
MIT License
2.73k stars 632 forks source link

Provide a way to really waitForAuth() #898

Closed dimitrovs closed 7 years ago

dimitrovs commented 7 years ago

Currently you have a method waitForAuth() but based on the description it doesn't look like it does what the name would imply (i.e. it doesn't "wait for the user to be authenticated and resolve only when the user is authenticated"). I wrote this method which I think could be a good addition to the library (you will probably have to call it something else):

        waitForAuth: function() {
            var deferred = $q.defer(),
                offHolder = {};
            if (authObj.$getAuth()) {
                deferred.resolve();
            }
            else {
                offHolder.offAuth = authObj.$onAuthStateChanged(function(authData) {
                    if (authData) {
                        deferred.resolve();
                        this.offAuth();
                    }
                }, offHolder);
            }
            return deferred.promise;
        },
jwngr commented 7 years ago

I think you are referring to $waitForSignIn() since $waitForAuth() is part of the 1.x.x version of AngularFire which uses the legacy Firebase SDKs (2.x.x and lower). The docs for $waitForSignIn() say the following:

Helper method which returns a promise fulfilled with the current authentication state. This is intended to be used in the resolve() method of Angular routers.

This is the correct description and the intended behavior of the method. I'm not sure where you got the "wait for the user to be authenticated and resolve only when the user is authenticated" phrasing. It might have been in an older version of our documentation. I agree that wording is confusing. I'm glad our wording is clearer today.

As for a method which actually wants until a user is authenticated, I don't really see a use case for that. Waiting for the auth state to resolve (as the $waitForSignIn() method does) makes sense and has lots of use cases though.