5
votes

I'm creating a new user with AngularFire. But when I sign the user up I also ask for first name and last name and I add that info after registration.

$firebaseSimpleLogin(fbRef).$createUser($scope.signupData.email, $scope.signupData.password).then(function (user) {
  // Add additional information for current user
  $firebase(fbRef.child('users').child(user.id).child("name")).$set({
    first: $scope.signupData.first_name,
    last: $scope.signupData.last_name
  }).then(function () {
    $rootScope.user = user;
  });
});

The above code works, it creates node fin Firebase (users/user.id/ ...).

The problem

When I login with the new user I get the user default information: id, email, uid, etc. but no name. How can I associate that data automatically to the user?

2

2 Answers

9
votes

You can't. Firebase hides the complexity of login management by storing the login details in its own datastore. This process knows nothing of your app's forge, which means it doesn't know if or where you're storing any additional user information. It returns the data that it does know about as a convenience (id, uid, email, md5_hash, provider, firebaseAuthToken).

It's up to your app to then take the [u]id and grab whatever app specific user information you need (such as first name, last name). For an Angular app, you'd want to have a UserProfile service which retrieves the data you're looking for once you get the authentication success broadcast.

Also, in your snippet, consider changing

.child(user.id) 

to

.child(user.uid) 

This will come in handy if you ever support Facebook/Twitter/Persona authentication later on. uid looks like "simplelogin:1" - it helps to avoid unlikely but possible id clashes across providers.

0
votes

I have the same issue on this and feel like noone actually has a clear answer (2 years on). But here is the rough structure of how such a service could look like:

app.factory('Auth', function(FURL, $firebaseAuth, $firebaseObject, $rootScope, $window){
​
  var ref = new Firebase(FURL);
  var auth = $firebaseAuth(ref);
​
  var Auth = {
    user: {},
​
    login: function(user){
      return auth.$authWithPassword({
        email: user.email,
        password: user.password
      });
    },
​
    signedIn: function(){
      return !!Auth.user.provider;
    },
​
    logout: function(){
      return auth.$unauth;
    }
  };
​
  // When user auths, store auth data in the user object
  auth.$onAuth(function(authData){
    if(authData){
      angular.copy(authData, Auth.user);
      // Set the profile

      Auth.user.profile = $firebaseObject(ref.child('profile').child(authData.uid));
      Auth.user.profile.$loaded().then(function(profile){
        $window.localStorage['gym-key'] = profile.gym.toString();
      });
    } else {
      if(Auth.user && Auth.user.profile){
        Auth.user.profile.$destroy();
      }
​
    }
  });
​
  return Auth;
});