Adding New Data to Firebase Users

Adding new data to firebase users

There is no way to add arbitrary additional data to Firebase Authentication user profiles. If you want that, consider using the Firebase Realtime Database (or Cloud Firestore) for storing the additional information.

This approach has been covered in quite a few questions in the past, so I'll link you to those:

  • Firebase: setting additional user properties
  • Add extra User Information with firebase
  • How do I link each user to their data in Firebase?
  • Swift & Firebase - How to store more user data other than email and password?
  • Store additional information during registration with Firebase in Android
  • How to add additional information to firebase.auth()

Since a few weeks ago you can add small bits of information to the Firebase Authentication user profile. While this might sound like what you need, it is explicitly not meant for storing user metadata such as you need. Instead this is intended for storing so-called claims: properties about the user that you then access in the security rules. See the documentation for setting custom claims.

Add extra User Information with firebase

First, create a users directory in db. Then, using user's unique id you get from authn process, store the user info under users/{userid}.

To achieve this, you need to get into the details of Firebase database. See here: https://firebase.google.com/docs/database/android/save-data

How to add additional information to firebase.auth()

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

Firebase - Adding additional user data to separate DB

  1. Initialization of firebase object was done for the old version, see this https://firebase.google.com/docs/web/setup , it uses firebase.initializeApp(config) instead of new firebase()

  2. to update your database with user's additional fields use this code

    firebase.database().ref('users/' + user.uid).set({
    firstName: firstName,
    lastName: lastName
    })

Creating Firebase User and simultaneously adding data into database

You should take a look at the return type of createUserWithEmailAndPassword. If I'm reading the docs correctly, it returns an instance of firebase.auth.UserCredential, not an actual user. I think you need to actually drill down one more level into that credential object and get the user.uid.

Example

firebase.auth().createUserWithEmailAndPassword(email, password)
.then(userCredential => {

//set data into User database
firebase.database().ref('Admin/Person In Charge' + "/" + userCredential.user.uid).set({
Name: name,
ContactNo: hp,
Email: email,
Role: role
}).then(success => {
console.log(user);
window.alert("Registered");
window.location = "user.html";
});
})

You could figure this out in the future by inspecting the value of your user in your then via a console.log().

Tried to add a new field of data to Firebase user when registering, but no update X method in FirebaseUser.class file

FirebaseUser class, has only a few fields that are related to the authentication. If you need more details about your users, you should create a POJO class to store additional data.

This data can be stored either in Cloud Firestore or in the Realtime Database so it can be later used.

Please also note, the Realtime Database doesn't store null values. Firestore does.

How do I add data from a user in firebase rather than replace what is already there?

That is expected, you basically are referencing the very same node child("quote") and trying to change its value. But if you want to have multiple quotes, what you need to do is to create multiple nodes under the parent child("Quotes") with different names.

One trivial way of doing so, you might append a different number to each new quote node, for example when you want to add a new quote, define the following path:

child("Quotes").child("quote1").setValue("...")

Path for another quote:

child("Quotes").child("quote2").setValue("...")
And so on.

Alternatively, you can use Firebase Database reference method childByAutoId() to generate unique names. You will use that method after defining the parent node:

ref!.child("users").child(Auth.auth().currentUser!.uid).child("Quotes").childByAutoId().setValue(quotesLabel.text!)


Note:

Try to avoid force unwrapping as much as you can because that makes your app more prone to crashes.



Related Topics



Leave a reply



Submit