Firebase Admin SDK不能如期更新密码

问题描述 投票:0回答:1

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

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

密码必须是一个 "原始的,未清洗的密码。必须至少有六个字符长"。

根据文档,我的云函数如下。

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: '[email protected]',
  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: '[email protected]',
       displayName: undefined,
       email: '[email protected]',
       photoURL: undefined,
       providerId: 'password',
       phoneNumber: undefined } ] }

javascript firebase firebase-authentication google-cloud-functions firebase-admin
1个回答
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"
})
© www.soinside.com 2019 - 2024. All rights reserved.