accountService.ts 1.05 KB
import { createApiClient } from "../lib/apiClient";
import { setTokens } from "../lib/authStorage";

export type AccountLoginInput = {
  userName: string;
  password: string;
  uuid?: string | null;
  code?: string | null;
};

export type AccountLoginOutput = {
  token: string;
  refreshToken?: string | null;
};

const api = createApiClient();

/**
 * ABP conventional: POST /api/app/account/login
 * (matches backend method name PostLoginAsync)
 */
export async function login(input: AccountLoginInput): Promise<AccountLoginOutput> {
  const raw = await api.requestJson<any>({
    path: "/account/login",
    method: "POST",
    body: {
      userName: input.userName,
      password: input.password,
      uuid: input.uuid ?? null,
      code: input.code ?? null,
    },
  });

  const token = String(raw?.token ?? raw?.Token ?? "");
  if (!token) throw new Error("Sign-in failed: no token was returned.");

  const refreshToken = (raw?.refreshToken ?? raw?.RefreshToken ?? null) as string | null;
  setTokens({ token, refreshToken });
  return { token, refreshToken };
}