繁体   English   中英

Firebase Admin SDK - 使用自定义字段创建用户

[英]Firebase Admin SDK - Create user with custom fields

我一直在关注Firebase SDK Admin - Manager User文档,一切都用它的系统找到了。 无论哪种方式,我都想使用名字、姓氏、国籍等字段来扩展我的用户信息。

现在我正在使用 Vue.JS(前端)、Axios(Http 客户端)和 Express.js(服务器)来管理我的项目,这就是我创建用户的方式:

Vue.JS createUser方法:

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
    }

每个字段: usernameemailfirstName等...从一个常见的 HTML 表单中获取其值,并且它们工作得很好。

这是我在服务器端的“用户创建”路由:

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

这就是我检索用户信息的方式:

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

问题是我无法获得我添加的自定义字段(或者它们根本不存在)。 这是我使用下一种方法获取数据后的图片:

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

在此处输入图片说明

有没有办法在我的用户创建中设置自定义字段,或者(如果我的流程是正确的)检索它们的方法?

Firebase 用户不可自定义,遗憾的是您无法向用户添加自定义字段...查看这些文档以了解此对象的属性 另请查看此问题和答案,其中 Frank 解释说您需要单独存储任何额外信息……这通常是在数据库中的表(命名为/userDetails )中完成的。

您可以在创建后立即设置用户的配置文件。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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