MarcoS
MarcoS

Reputation: 17711

Upgrading to Firebase 1.1: can't get user.uid from createUser()

With Firebase 1.0.21 and $firebaseSimpleLogin - to register a user - I did something like this:

app.controller('AuthCtrl', function ($scope, $firebase, $firebaseSimpleLogin) {
  var ref = new Firebase(MY_FIREBASE_URL);
  var auth = $firebaseSimpleLogin(ref);

  $scope.register = function (valid) {
    // user contains email ad password fields, from my view
    auth.$createUser($scope.user.email, $scope.user.password).then(function (auth) {
      // auth.user.uid is defined, here; for example: 'simplelogin:13'
      // ...
    });
  };
};

With Firebase 1.1, $firebaseSimpleLogin has been deprecated, and it's functionalities incorporated in Firebase core.
So I - trustful I was :-) - changed code this way:

app.controller('AuthCtrl', function ($scope, $firebase) {
  var ref = new Firebase(MY_FIREBASE_URL);
  var auth = $firebase(ref);

  $scope.register = function (valid) {
    // user contains email ad password fields, from my view
    auth.$createUser($scope.user.email, $scope.user.password).then(function (auth) {
      // ...
    });
  };      
};

Though, I get $createUser() as 'undefined'...
So I did try:

app.controller('AuthCtrl', function ($scope, $firebase) {
  var ref = new Firebase(MY_FIREBASE_URL);
  var auth = $firebase(ref);

  $scope.register = function (valid) {
    // user contains email ad password fields, from my view
    ref.createUser({
      email: $scope.user.email,
      password: $scope.user.password
    }, function(err) {
      if (!err) {
        // ??? how do I access uid from here ???
      }
    });
  };      
};

I did hope ref.createUser() should return a promise, like auth.$createUser did, but it doesn't.
So, I don't know hot to get my fresh user uid, to insert it in my Users extended info objects array...

Upvotes: 0

Views: 293

Answers (1)

Leandro Zubrezki
Leandro Zubrezki

Reputation: 1150

AngularFire depends on "firebase": "1.0.x" and "firebase-simple-login": "1.6.x", for this reason, the authentication methods on the core of Firebase are not yet implemented in AngularFire.

You have two options:

Not upgrade Firebase and continue using the version 1.0.x with AngularFire and Firebase Simple Login

Or, upgrade Firebase to 1.1 but then you have to remove AngularFire and Firebase Simple Login and only use the Firebase methods. Since you are using AngularJS, maybe you will need to wrap the Firebase methods in functions with promises using $q service and you will need to use $scope.$apply in some cases because depending on what you are doing Angular didn't know that the $scope has change and didn't trigger those changes.

$apply API Reference: https://docs.angularjs.org/api/ng/type/$rootScope.Scope

Firebase 1.1 API Reference: https://www.firebase.com/docs/web/api/

Upvotes: 1

Related Questions