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?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
enbermudas
  • 1,603
  • 4
  • 20
  • 42

2 Answers2

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.

JeremyW
  • 5,157
  • 6
  • 29
  • 30
  • Worked just fine! Thanks for your help. – enbermudas Jun 29 '18 at 20:01
  • I don't know if it is too late to ask but how do i get that information while listing my users ? – enbermudas Jun 29 '18 at 20:10
  • 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](https://firebase.google.com/docs/database/admin/retrieve-data) to the docs that explains how to query from the Admin-SDK – JeremyW Jun 29 '18 at 20:15
  • @JeremyW What about using the [setDisplayName](https://firebase.google.com/docs/reference/android/com/google/firebase/auth/UserProfileChangeRequest.Builder#public-userprofilechangerequest.builder-setdisplayname-string-displayname) 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? – Taha Sami Feb 03 '22 at 13:04
  • 1
    @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](https://firebase.google.com/docs/auth/admin/custom-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. – JeremyW Feb 03 '22 at 14:52
  • @JeremyW I don't want to store custom fields in Firestore because I want to escape from problems like failure connection and also it does not support transactions between Firestore and Auth. – Taha Sami Feb 03 '22 at 15:10
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
        })
    }
}
Alexander
  • 1,220
  • 6
  • 12