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
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.
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
Flutter Firebase adding other user information such as Address to the database
You have a lot of questions hidden in a single question. I'll try to address as many as I can below, but please try to stick to a single question in the future.
Recommending one database over the other is off-topic on Stack Overflow. But I'd recommend reading the Firebase documentation on it here.
Since you started with Cloud Firestore, let's focus on that.
You'll need to import the plugin for Firestore, just like you've done with the plugin with Authentication. So something like:
import 'package:cloud_firestore/cloud_firestore.dart';
As you've discovered, there is no AuthResult.uid
property. In such cases, I recommend browsing the reference documentation for the plugin, which makes it fairly easy to see that AuthResult.user
is probably the path to go.
So something like:
AuthResult result = await
FirebaseAuth.instance.createUserWithEmailAndPassword(email: _email,password:_password);
User user = result.user;
Not the solution to the question you're asking, but please use document(user.uid)
instead of just document()
. What you now have generates a document ID, while it's idiomatic (and much easier) to use the UID as the key in a users
collection.
How to to add extra user information to Firebase
There really isn't a schema. You write the values into a heirarcy you want. First you reference the UID.
In Swift it would be:
var usersRef = ref.childByAppendingPath("users")
Then you would create an object with all values you want to write. You could also write the values directly without making an object first.
let newUser = [
"provider": authData.provider,
"displayName": authData.providerData["displayName"] as? NSString as? String
]
Then write the values with:
ref.childByAppendingPath("users")
.childByAppendingPath(authData.uid).setValue(newUser)
The docs are tricky to follow. The reference for this example is https://www.firebase.com/docs/ios/guide/user-auth.html
This block of code will give you:
{
"users": {
"6d914336-d254-4fdb-8520-68b740e047e4": {
"displayName": "alanisawesome",
"provider": "password"
},
"002a448c-30c0-4b87-a16b-f70dfebe3386": {
"displayName": "gracehop",
"provider": "password"
}
}
}
Hope this helps!
How can I see and edit the User's Additional information from Firebase consol that was edited programmatically
The Firebase console shows only limited information about each user profile. The rest of the information is only available when you access the profile through a Firebase API, or export the data.
Keep in mind that Firebase provides Admin SDKs, which you can run on your development machine to accomplish simple administrative tasks such as this.
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
})
Related Topics
Facebook Key Hash Does Not Match Any Stored Key Hashes
Tools: Replace Not Replacing in Android Manifest
How to Take a Screenshot of a Current Activity and Then Share It
Android Listview Refresh Single Row
Hide/Show Bottomnavigationview on Scroll
Android Textwatcher.Aftertextchanged VS Textwatcher.Ontextchanged
Gradle - Library Duplicates in Dependencies
How to Get an Event in Android Spinner When the Current Selected Item Is Selected Again
Android Mediaplayer Error (1, -2147483648)
Create a Ninepatch/Ninepatchdrawable in Runtime
Open an Image Using Uri in Android's Default Gallery Image Viewer
Can't Get Write_Settings Permission
Share Sqlite Database Between 2 Android Apps
"Parse Error: There Is a Problem Parsing the Package" While Installing Android Application
How to Upload an Image File in Retrofit 2