planetoftheweb
planetoftheweb

Reputation: 222

Firebase's AngularFire Authentication $onAuth throwing "Undefined is not a function"

I'm trying to upgrade an application to the new Firebase Authentication methods. I've got most things working, but for some reason when I try this.

myApp.controller('StatusController', function(
  $scope, $rootScope,
    $location, $firebase, $firebaseAuth, FIREBASE_URL) {

  $scope.logout = function() {
    Authentication.logout();
    $location.path('/login');
  } //logout

  $rootScope.$onAuth(function(authUser) {
    var ref = new Firebase(FIREBASE_URL + '/users/' + authUser.uid);
    var user = $firebase(ref).$asObject();

    user.$loaded().then(function() {
      $rootScope.currentUser = user;
    });
  }); //login

  $rootScope.$on('logout', function(e, authUser) {
    $rootScope.currentUser = null;
  }); //login

}); //StatusController

I'm getting a "undefined is not a function", even when I just try the example on the site. I'm not sure why this is happening. Just for reference, this controller keeps track of the authentication in order to update the site navigation.

<header>
  <nav class="cf" ng-include="'views/nav.html'"
    ng-controller="StatusController"></nav>
</header>

Upvotes: 1

Views: 2347

Answers (2)

Petros Kyriakou
Petros Kyriakou

Reputation: 5343

you can fix the problem like this, ideally you would use $rootScope but this way it works too without the use of $rootScope as it seems Firebase staff implemented $onAuth in a rootScope-like way.

As an example to your application:

myApp.controller('StatusController', function($scope,$firebase,$firebaseAuth,FIREBASE_URL) {
    var ref = new Firebase(FIREBASE_URL);
    var auth = $firebaseAuth(ref);

    auth.$onAuth(function(authData){
        console.log(authData);
        $scope.userStatus = authData.password.email;
    }); // Check user status 
}); // StatusController

PS: authData.password.email only inserts the email address to the $scope.userStatus.

Upvotes: 3

AMG
AMG

Reputation: 704

Wrap the $onAuth function with $timeout so it processes in the digest loop!

// wrap the $onAuth function with $timeout so it processes
    // in the digest loop.
    onAuth: function onLoggedIn(callback) {
      auth.$onAuth(function(authData) {
        $timeout(function() {
          callback(authData);
        });
      });
    }

Upvotes: 0

Related Questions