如何使用 Firebase Rest API 更改密码?

问题描述

我正在尝试使用 Rest API post 请求更改用户密码,如下所示:

export const changePassword = async (userId,token,expirationTime,password) => {

  const response = await fetch(
    'https://identitytoolkit.googleapis.com/v1/accounts:update?key=[MY_KEY]',{
      method: 'POST',headers: {
        'Content-type': 'application/json',},body: JSON.stringify({
        idToken: token,password: password.newPassword,returnSecuretoken: true,}),);

  if (!response.ok) {
    const errorResData = await response.json();
    const errorId = errorResData.error.message;
    let message = 'Something went wrong!';

    if (errorId === 'EMAIL_NOT_FOUND') {
      message = 'This e-mail can not be found.';
    } else if (errorId === 'INVALID_PASSWORD') {
      message = 'This password is invalid!';
    }

    throw new Error(message);
  }

  const resData = await response.json();
  console.log(resData);

  alert('Password changed successfully!')
};

我正确地获得了 idToken,但我不知道下一步该怎么做。我的 resData 如下所示:

email: "[email protected]"
emailVerified: false
kind: "identitytoolkit#SetAccountInfoResponse"
localId: "FXPwA0n1f1RIqliLOx0tLkhgORy1"
passwordHash: "UkVEQUNURUQ="
providerUserInfo: Array(1)

我知道我需要使用 refreshToken 做某事但需要帮助。 这是用于身份验证的 Firebase Rest API 文档:https://firebase.google.com/docs/reference/rest/auth#section-api-usage

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)