Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase User Profile Add Custom Fields

Is it possible to add custom fields to the user profile table in Firebase?

Right now it looks like I can only add data to:

  • "uid"
  • "displayName"
  • "photoURL"
  • "email"
  • "emailVerified"
  • "phoneNumber"
  • "isAnonymous"
  • "tenantId"
  • "providerData":
  • "apiKey"
  • "appName"
  • "authDomain"
  • "stsTokenManager"
  • "redirectEventId"
  • "lastLoginAt"
  • "createdAt"

I would like to have the ability to add custom objects to the JSON string to contain all of the user data.

https://firebase.google.com/docs/auth/web/manage-users

like image 561
Olivia Avatar asked Oct 24 '19 19:10

Olivia


People also ask

How do I add more fields in Firebase authentication?

If you want to add additional data, you need to create a model class for your user and then store it in your Firebase database. What about using the setDisplayName method to store custom fields as a JSON Object like this example {"display_name": "User 1", "age": 25, "gender": "Male", "points": 5371} ?

Is Firebase user UID unique?

Firebase users have a fixed set of basic properties—a unique ID, a primary email address, a name and a photo URL—stored in the project's user database, that can be updated by the user (iOS, Android, web).

What is onAuthStateChanged in Firebase?

According to the documentation, the onAuthStateChanged() function returns. The unsubscribe function for the observer. So you can just: var unsubscribe = firebase.


1 Answers

Quick example, where after I have registered a user, I add that user to a usersCollection and supply other field information as necessary.

In my firebase powered app, what I do is the following:

import app from 'firebase/app'
import 'firebase/auth'
import 'firebase/database'
import 'firebase/firestore'

const config = {
  apiKey: process.env.REACT_APP_API_KEY,
  authDomain: process.env.REACT_APP_AUTH_DOMAIN,
  databaseURL: process.env.REACT_APP_DATABASE_URL,
  projectId: process.env.REACT_APP_PROJECT_ID,
  storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
  messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID 
}

class Firebase {
  constructor (props) {
    app.initializeApp(config)

    this.auth = app.auth()
    this.db = app.database()
    this.firestore = app.firestore()
  }

  signUpWithEmailAndPassword = (email, pw) => {
    this.auth.createUserWithEmailAndPassword(email, password)
    .then(registeredUser => {
      this.firestore.collection("usersCollection")
      .add({
        uid: registeredUser.user.uid,
        field: 'Info you want to get here',
        anotherField: 'Another Info...',
        ....
      })
    }
  }
}

Hope this helps.

like image 191
Angelo Avatar answered Sep 21 '22 00:09

Angelo