passwordResetService.ts 1.45 KB
import { ApiError, createApiClient } from "../lib/apiClient";

const api = createApiClient();

/**
 * 发送忘记密码邮箱验证码。
 * 预留路径:`POST /api/app/account/send-password-reset-code`(后端接入后对齐方法名与 DTO 即可)
 */
export async function sendPasswordResetCode(email: string, signal?: AbortSignal): Promise<void> {
  try {
    await api.requestJson<unknown>({
      path: "/account/send-password-reset-code",
      method: "POST",
      body: { email },
      signal,
    });
  } catch (e) {
    if (e instanceof ApiError && (e.status === 404 || e.status === 501)) {
      throw new Error("Password reset is not available on the server yet. Please contact an administrator.");
    }
    throw e;
  }
}

/**
 * 验证码 + 新密码完成重置。
 * 预留路径:`POST /api/app/account/confirm-password-reset`
 */
export async function confirmPasswordReset(
  input: { email: string; code: string; newPassword: string },
  signal?: AbortSignal,
): Promise<void> {
  try {
    await api.requestJson<unknown>({
      path: "/account/confirm-password-reset",
      method: "POST",
      body: {
        email: input.email,
        code: input.code,
        newPassword: input.newPassword,
      },
      signal,
    });
  } catch (e) {
    if (e instanceof ApiError && (e.status === 404 || e.status === 501)) {
      throw new Error("Password reset is not available on the server yet. Please contact an administrator.");
    }
    throw e;
  }
}