简体   繁体   中英

Firebase Admin SDK - Create user with custom fields

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)
      })
    },

在此处输入图片说明

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM