【问题标题】:Is it possible to set the displayName in Firebase for the anonymous authentication是否可以在 Firebase 中为匿名身份验证设置 displayName
【发布时间】:2014-03-29 02:47:10
【问题描述】:

我正在使用 Firebase 的通过 AngularFire 的简单登录和匿名登录方法,我想知道是否有办法设置从返回的用户对象的 displayName 属性

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

我想设置 displayName 并将其保存回 Firebase,以便用户可以显示为该 displayName 属性。据我所知,您自己实际上并没有向 Simple Login 写任何东西。似乎它唯一一次被写入是如果您使用诸如电子邮件/密码身份验证之类的东西并使用$scope.loginObj.$createUser('name', 'password')

【问题讨论】:

    标签: angularjs firebase angularfire


    【解决方案1】:

    这在您描述的方式中是不可能的,但是,您可以这样做以获得相同的行为。

    $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");
    

    您甚至可以使用简单的安全规则来备份:

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

    这确保只有经过正确身份验证的用户才能修改自己的显示名称。

    【讨论】:

    • 这其实是一个相当合理的做法,谢谢建议!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    • 2016-12-22
    • 2016-06-08
    • 2017-06-03
    相关资源
    最近更新 更多