I'm new to Firebase and am using the email/password sign-in method. I have the method working fine however on my signup form I have additional fields I want to enter in the database. My understanding from the email/password signup method is that any additional fields will have to be stored separately to how the email/ password auth is stored when a user signs up.
I have created in my databases a sub-section to store additional user details called users. here's how it looks in firebase:
Here is the function i'm calling (in React) to create a new user:
signUp(e) {
e.preventDefault();
var firstName = $('.signup-first-name').val();
var lastName = $('.signup-last-name').val();
var userName = $('.signup-user-name').val();
var password = $('.signup-user-password').val();
var email = $('.signup-email').val();
var auth = firebase.auth();
const promise = auth.createUserWithEmailAndPassword(email,password).then(function(user) {
var user = firebase.auth().currentUser;
user.updateProfile({
displayName: userName,
}).then(function() {
// Update successful.
// new db code here
var ref = new firebase("https://music-app-7a4d3.firebaseio.com");
ref.onAuth(function(authData) {
ref.child("users").child(authData.uid).set({
firstName: firstName,
lastName: lastName
})
})
// end new db code here
}, function(error) {
// An error happened.
});
}, function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode == 'auth/weak-password') {
alert('The password is too weak.');
} else {
console.error(error);
}
});
promise.catch(e => console.log(e.message));
firebase.auth().onAuthStateChanged(firebaseUser => {
if(firebaseUser) {
console.log(firebaseUser)
} else {
console.log("not logged in")
}
});
}
Have followed another example I found on here:
Add Extra Details on Firebase User Table
but not sure where i've gone wrong along the way. Documentation on this is fairly weak which doesn't help!
Append to a list of data. Use the push() method to append data to a list in multiuser applications. The push() method generates a unique key every time a new child is added to the specified Firebase reference.
Create multiple Realtime Database instances If you're on the Blaze pricing plan, you can create multiple database instances in the same Firebase project. In the Firebase console, go to the Data tab in the Develop > Database section. Select Create new database from the menu in the Realtime Database section.
Add a memberSign in to Firebase. Click. , then select Permissions. On the Permissions page, click Add member.
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()
to update your database with user's additional fields use this code
firebase.database().ref('users/' + user.uid).set({
firstName: firstName,
lastName: lastName
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With