Files
ctms/frontend/src/api/authClient.ts
T
Cheng Zhou 7c721d4e5c
Client Quality Gates / Shared client and Web (push) Has been cancelled
Client Quality Gates / macOS Desktop (push) Has been cancelled
refactor(client): unify web and desktop release workflow
2026-06-30 20:25:07 +08:00

51 lines
1.2 KiB
TypeScript

import axios from "axios";
import type { AxiosResponse } from "axios";
import { clientRuntime, DESKTOP_SERVER_URL_CHANGED_EVENT } from "../runtime";
const authClient = axios.create({
baseURL: clientRuntime.apiBaseUrl(),
timeout: 15000,
});
export const refreshAuthClientBaseUrl = (): void => {
authClient.defaults.baseURL = clientRuntime.apiBaseUrl();
};
if (typeof window !== "undefined") {
window.addEventListener(DESKTOP_SERVER_URL_CHANGED_EVENT, refreshAuthClientBaseUrl);
}
export type ExtendResponse = {
accessToken: string;
expiresAt: string;
};
export type LoginKeyResponse = {
key_id: string;
public_key: string;
challenge: string;
expires_at: string;
};
export type EncryptedPasswordRequest = {
key_id: string;
challenge: string;
ciphertext: string;
};
export const extendToken = (token: string): Promise<AxiosResponse<ExtendResponse>> =>
authClient.post<ExtendResponse>(
"/api/v1/auth/extend",
{},
{
headers: {
Authorization: `Bearer ${token}`,
},
}
);
export const getLoginKey = (): Promise<AxiosResponse<LoginKeyResponse>> =>
authClient.get<LoginKeyResponse>("/api/v1/auth/login-key");
export default authClient;