How to add additional information to firebase.auth()

后端 未结 3 1962
梦毁少年i
梦毁少年i 2020-11-29 10:30

How can I add extra attributes phone number and address to this data set? It seems like Firebase documentation doesn\'t specify anything about that.

I have implemen

相关标签:
3条回答
  • 2020-11-29 10:57

    You could write some code that combines data from firebase auth and firestore document and expose that to the app as a single data entity. To take subscriptions and notify that changes to the whole app, you would be better served with event libraries like Rxjs. Bellow, I wrote the example below using a simple library that implements an event bus.

    // 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));
    

    You can expand on that in a post I wrote detailing this solution and also talking about how to update those properties. There is too a small library that encapsulates the answer with some other minor features with code you could check.

    0 讨论(0)
  • 2020-11-29 10:59

    This can be done by directly storing your custom data in Firebase Auth as "custom claims" on each user via the Admin SDK on your backend.

    Note this can't be done purely client-side, your server (or you can use a Cloud Function as per the linked guide if you don't already have a server/API set up) needs to make a request through the Admin SDK to securely set the data using the admin.auth().setCustomUserClaims() method:

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

    0 讨论(0)
  • 2020-11-29 11:23

    As far as I know, you have to manage the users profiles by yourself if you want to have more fields than the default user provided by Firebase.

    You can do this creating a reference in Firebase to keep all the users profiles.

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

    You can use onAuthStateChanged to detect when the user is logged in, and if it is you can use once() to retrieve user's data

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

    Hope it helps

    0 讨论(0)
提交回复
热议问题