Pregunta

I'm using Firebase's Simple Login through AngularFire with the anonymous login method and I was wondering if there's anyway to set the displayName property of the user object returned from

$scope.loginObj.$login('anonymous').then(function(user) {
  user.displayName // This is an empty string
}

I would like to set displayName and save it back in Firebase so that users can be shown as that displayName property. As far as I know you don't actually write anything to Simple Login yourself. It seems like the only time it's written to is if you're using something like email/password authentication and using $scope.loginObj.$createUser('name', 'password')

¿Fue útil?

Solución

This isn't possible in the way you describe, however, you can do this to get the same behavior.

$scope.loginObj.$login('anonymous').then(function(user) {
  if (!user) return;
  $scope.userRef = (new Firebase('<Your Firebase>.firebaseio.com/users/')).child(user.uid);
  $scope.userRef.child('displayName').on('value', function (snapshot) {
    user.displayName = shapshot.val();
  });
});

// Then elsewhere in your code, set the display name and user.displayName will be updated automatically

$scope.userRef.child('displayName').set("DISPLAY NAME");

You can even back this up with simple Security Rules:

{"rules":
  "users": {
    "$uid" {
      ".read": true,
      ".write": "$uid == auth.uid'
    }
  }
}

This ensures that only a correctly authenticated user can modify their own display name.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top