如何向 firebase.auth() 添加附加信息

How to add additional information to firebase.auth()

如何向该数据集添加额外的属性 phone 号码和地址? 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 中创建一个引用以保留所有用户配置文件。

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

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

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

希望对您有所帮助

您可以编写一些代码来组合来自 firebase auth 和 firestore 文档的数据,并将其作为单个数据实体公开给应用程序。要订阅并通知整个应用程序的更改,您最好使用实现事件总线的 Rxjs. Bellow, I wrote the example below using a 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 I wrote detailing this solution and also talking about how to update those properties. There is too a small library 中对此进行扩展,该 post I wrote detailing this solution and also talking about how to update those properties. There is too a small library 将答案与您可以检查的代码的其他一些次要功能封装在一起。

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

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

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