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()
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 datafirebaseRef.child('users').child(user.uid).once('value', callback)
Hope it helps Firebase - Adding additional user data to separate DB
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 ofnew firebase()
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
How to Auto-Slide the Window Out from Behind Keyboard When Textinput Has Focus
Npm Start Error with Create-React-App
How to Detect When a Tab Is Focused or Not in Chrome with JavaScript
Updating Svg Element Z-Index with D3
Javascript: Do Primitive Strings Have Methods
JavaScript Time Zone Is Wrong for Past Daylight Saving Time Transition Rules
Professional Jquery Based Combobox Control
JavaScript Call Nested Function
JavaScript Remove "Disabled" Attribute from HTML Input
Dynamic Require in Requirejs, Getting "Module Name Has Not Been Loaded Yet for Context" Error
Bootstrap: Open Another Modal in Modal
Removing Duplicate Objects with Underscore for JavaScript
When to Use Preventdefault( ) VS Return False
Why Let and Var Bindings Behave Differently Using Settimeout Function
React Doesn't Reload Component Data on Route Param Change or Query Change
Regular Expression to Match Exactly 5 Digits