【问题标题】:Firebase Admin SDK not updating password as expectedFirebase Admin SDK 未按预期更新密码
【发布时间】:2020-09-05 15:23:21
【问题描述】:

我正在使用云功能,并且正在尝试手动覆盖用户密码(我还设置了密码电子邮件重置)。

我可以成功更新用户编号、启用/禁用状态等,但是在传递密码变量时它似乎失败了,没有错误消息。

密码必须是“原始的、未散列的密码”。长度必须至少为六个字符。'

按照文档,我的云功能如下。

exports.resetUserPassword = functions.https.onCall(async (data, context) => {

  if (!context.auth.token.superadmin) return

  try {
    console.log(data.password)
    admin.auth().updateUser(data.uid, {
      password: 'testpassword',
    })
      .then(function(userRecord) {
        // See the UserRecord reference doc for the contents of userRecord.
        console.log("Successfully updated user", userRecord.toJSON());
      })
      .catch(function(error) {
        console.log("Error updating user:", error);
      });

    return true
  } catch (error) {
    console.log(error)
  }

});

当我查看 firebase 功能的日志时,响应似乎表明密码尚未更新(当然,当我尝试使用测试帐户登录时,密码已设置为某个值...IDK ....)。注意passwordHash和passwordSalt是未定义的,

Successfully updated user { uid: 'HMxo5NcObYTN5LPsgSb0ZTCK6213',
  email: 'test@test.com',
  emailVerified: false,
  displayName: undefined,
  photoURL: undefined,
  phoneNumber: undefined,
  disabled: false,
  metadata: 
   { lastSignInTime: 'Tue, 19 May 2020 00:36:16 GMT',
     creationTime: 'Tue, 19 May 2020 00:36:16 GMT' },
  passwordHash: undefined,
  passwordSalt: undefined,
  customClaims: { customer: true },
  tokensValidAfterTime: 'Tue, 19 May 2020 13:50:02 GMT',
  tenantId: undefined,
  providerData: 
   [ { uid: 'test@test.com',
       displayName: undefined,
       email: 'test@test.com',
       photoURL: undefined,
       providerId: 'password',
       phoneNumber: undefined } ] }

【问题讨论】:

  • 我不清楚您是否遇到错误。你写“它似乎失败了,没有错误消息。”,然后你提到似乎是错误消息“密码必须是'原始的、未散列的密码。必须至少六个字符长。”那么您说“当然,当我尝试使用测试帐户登录时,密码已设置为某些东西”?那么请问您的具体问题是什么?
  • 这不是错误信息,是参考文档。运行时没有报错,但密码保持不变
  • Sorry 被更改为未知值

标签: javascript firebase firebase-authentication google-cloud-functions firebase-admin


【解决方案1】:

您的代码忽略了updateUser().then().catch() 返回的承诺。在所有异步工作完成后,可调用函数需要返回一个承诺,该承诺通过发送给调用者的响应来解决。现在,您的函数仅返回 true 并在 updateUser 的工作实际完成之前立即完成。

为了让工作完成,你应该返回承诺链,并指出调用者应该收到什么:

return admin.auth().updateUser(...)
.then(() => {
    return "whatever you want the client to receive on success"
})
.catch(error => {
    return "whatever you want the client to receive on error"
})

【讨论】:

  • 这对我不起作用。我尝试在节点外壳中调用updateUser,并观察到它正确更新了displayName 等字段,但passwordHashpasswordSalt 字段仍然未定义
猜你喜欢
  • 1970-01-01
  • 2021-11-22
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多