【问题标题】:How to add additional information to firebase.auth()如何向 firebase.auth() 添加附加信息
【发布时间】:2016-10-21 22:53:55
【问题描述】:

如何向此数据集添加额外的属性电话号码和地址?似乎 Firebase 文档没有具体说明。

我已经使用firebase.auth()实现了登录、注册和更新

登录:

//Email Login
firebase.auth().signInWithEmailAndPassword(email, password).then(
   ok => {
        console.log("Logged in User",ok.user);              
    },
    error => {
        console.log("email/pass sign in error", error);
    }
);

注册:

 //Sign Up
firebase.auth().createUserWithEmailAndPassword(email, password).then(
    ok => {
        console.log("Register OK", ok);
    },
    error => {
        console.log("Register error", error);
    }
)

更新:

//User Authentication
firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    $scope.data=user;
  } else {
    // No user, Redirect to login page
  }
});

//Save Function
$scope.save=function(values){

    $scope.data.updateProfile({

      displayName: "Test User",
      email: "test@gmail.com",
     /* phone: 123412341,
      address: "Temp Address",*/
      photoURL: "www.example.com/profile/img.jpg"

    }).then(function() {

     // Update successful.

    }, function(error) {

     // An error happened.

    }); 

};  

【问题讨论】:

标签: firebase firebase-realtime-database firebase-authentication


【解决方案1】:

这可以通过后端的 Admin SDK 将您的自定义数据直接存储在 Firebase Auth 中作为每个用户的“自定义声明”来完成。

请注意,这不能纯粹在客户端完成,您的服务器(或者如果您还没有设置服务器/API,则可以按照链接指南使用云功能)需要通过Admin SDK 使用admin.auth().setCustomUserClaims() 方法安全地设置数据:

https://firebase.google.com/docs/auth/admin/custom-claims#defining_roles_via_an_http_request

【讨论】:

  • 这正是我想要的。虽然我的用例与问题中的用例不同(需要针对用户存储来自另一个系统的 UID),但这是完美的。基本上是根据授权记录存储自定义元数据。
【解决方案2】:

您可以编写一些代码来组合来自 firebase auth 和 firestore 文档的数据,并将其作为单个数据实体公开给应用程序。要订阅并通知整个应用程序的更改,您最好使用Rxjs 之类的事件库。下面,我使用实现事件总线的simple library 编写了下面的示例。

// auth.js
import { publish } from '@joaomelo/bus'
import { fireauth, firestore } from './init-firebase.js'

const authState = {
  userData: null
};

fireauth.onAuthStateChanged(user => {
  if (!user) {
    authState.userData = null;
    publish('AUTH_STATE_CHANGED', { ...authState });
    return;
  }

  // we must be carefull
  // maybe this doc does not exists yet
  const docRef = firestore
    .collection('profiles')
    .doc(user.uid);

  docRef
    // 'set' secures doc creation without 
    // affecting any preexisting data
    .set({}, { merge: true }) 
    .then(() => { 
      docRef.onSnapshot(doc => {
        // the first data load
        // and subsequent updates
        // will trigger this
        authState.userData = {
          id: user.uid, 
          email: user.email,
          ...doc.data()
        };
        publish('AUTH_STATE_CHANGED', { ...authState });
      });
    });  
});

// some-place-else.js
import { subscribe } from '@joaomelo/bus'
subscribe('AUTH_STATE_CHANGED', 
  authState => console.log(authState));

您可以在post 中对此进行扩展,我详细介绍了此解决方案并讨论了如何更新这些属性。还有一个small library 将答案与其他一些次要功能封装在一起,您可以检查代码。

【讨论】:

    【解决方案3】:

    据我所知,如果您想拥有比 Firebase 提供的默认用户更多的字段,则必须自己管理用户配置文件。

    您可以在 Firebase 中创建一个引用来保存所有用户的个人资料。

    users: {
      "userID1": {
        "name":"user 1",
        "gender": "male" 
      },
      "userID2": {
        "name":"user 2",
        "gender": "female" 
      }
    }
    

    您可以使用onAuthStateChanged 来检测用户何时登录,如果是,您可以使用once() 来检索用户的数据

    firebaseRef.child('users').child(user.uid).once('value', callback)
    

    希望对你有帮助

    【讨论】:

    • 目前这是一个很好的解决方案。但我很高兴知道是否有更好的解决方案
    • 这是建议的方法。更好的解决方案可能是更适合您需求的解决方案。您应该根据您想要检索它们的方式来构建您的数据。例如,如果你想保留一些信息的秘密,你可以这样做:“userId”:{“credit-card”:“0000”,“public”:{“name”:“Devid”,“genere”:“dragon” }} 并使用安全规则允许其他用户只读取“公共”节点。希望对您有所帮助:) 我来这里是为了解决任何其他问题
    • 这个答案可能会有所帮助:stackoverflow.com/a/37420701/4695325
    猜你喜欢
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多