Firebase Admin SDK 未按预期更新密码

Firebase Admin SDK not updating password as expected

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

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

密码必须是 'raw, unhashed password. Must be at least six characters long.'

按照文档,我的云函数如下。

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 } ] }

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

为了完成工作,您应该 return 承诺链,并指出调用者应该收到什么:

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