3

I've been following the Firebase SDK Admin - Manager User docs and everything went find for me with it's system. Either way, i want to extend my users information with fields such as first name, last name, nacionality, etc etc.

Right now i'm using Vue.JS (fronted), Axios (Http client) and Express.js (server) to manage my project and this is the way i create my users:

Vue.JS createUser method:

createUser() {
      axios.post('http://localhost:3000/users',
      {
        username: this.username,
        email: this.email,
        firstName: this.firstName,
        lastName: this.lastName,
        password: this.password,
      }).then(r => {
        console.log(r)
      }).catch(e => {
        console.log(e)
      })
      // createUser
    }

Each field: username, email, firstName, etc... got its value form a common HTML Form and they work just fine.

This is my "user create" route on the server side:

router.post('/', function (req, res, next) {
  firebase.auth().createUser({
    email: req.body.email,
    username: req.body.username,
    firstName: req.body.firstName,
    lastName: req.body.lastName,
    emailVerified: false,
    password: req.body.password,
    disabled: false
  }).then(function(userRecord) {
    console.log("Successfully created new user:", userRecord.uid);
  }).catch(function(error) {
    console.log("Error creating new user:", error);
  });
});

And this is how i retrieve the users information:

router.get('/', function(req, res, next) {
  firebase.auth().listUsers()
  .then(function(listUsersResult) {
    res.send(listUsersResult.users);
  }).catch(function(error) {
    console.log("Error listing users:", error);
  })
});

The problem is that i can't get the custom fields i added (or they simply does not exists). Here's is a pictures of the data once i get it with the next method:

getItems() {
      axios.get('http://localhost:3000/users').then(r => {
        this.users = r.data
      }).catch(e => {
        console.log(e)
      })
    },

enter image description here

Is there a way to set custom fields on my user creation or (if my process is right) a way to retrieve them?

2 Answers 2

4

Firebase users are not customizable, unfortunately you can not add custom fields to the user... check out these docs for the properties of this object. Also check out this question & answer where Frank explains that you need to store any extra info separately... this is typically done in a table (named something like /userDetails) in your database.

FEBRUARY 2022 EDIT:

Since writing this answer, custom user claims are now a thing. It's meant for authentication / rules purposes and not data-storage, so most things should still be kept in the database as originally stated. Note that you CAN access the custom fields client-side.

Keep in mind, this is adding additional data to the ID token that gets sent with EVERY server request, so you should try to limit usage of this to role/permission based things and not, say, image data for a 2nd profile avatar. Read the best practices for custom claims on the official docs.

Sign up to request clarification or add additional context in comments.

6 Comments

Worked just fine! Thanks for your help.
I don't know if it is too late to ask but how do i get that information while listing my users ?
In addition to making a query to get the default user data from the auth portion of firebase... you'd also have to make a query to the database portion of Firebase. Here is the link to the docs that explains how to query from the Admin-SDK
@JeremyW 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 it good practice? And what is the maximum of characters can I store inside display_name?
@TahaSami I can't find any docs on the max DisplayName length, in theory that should absolutely work. Also, since writing this answer, custom user claims are now a thing. It's meant for authentication / rules purposes, so I don't know if you can repurpose it for regular use... but if the info you want to store can change (age? points: 5371? even gender in 2022 now?) then it really belongs in a database.
|
1

You can set a user's profile, immediately after creating.

firebase.auth().createUserWithEmailAndPassword(
    email,
    password,
).then(credential => {
    if (credential) {
        credential.user.updateProfile({
            displayName: username
        })
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.