77 lines
1.9 KiB
TypeScript
77 lines
1.9 KiB
TypeScript
import axios from "axios";
|
|
import type { AxiosResponse } from "axios";
|
|
import { clientRuntime, DESKTOP_SERVER_URL_CHANGED_EVENT, getAppMetadataHeaders } 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);
|
|
}
|
|
|
|
authClient.interceptors.request.use((config) => {
|
|
config.headers = config.headers || {};
|
|
Object.assign(config.headers, getAppMetadataHeaders());
|
|
return config;
|
|
});
|
|
|
|
export type ExtendResponse = {
|
|
accessToken: string;
|
|
expiresAt: string;
|
|
};
|
|
|
|
export type LoginKeyResponse = {
|
|
key_id: string;
|
|
public_key: string;
|
|
challenge: string;
|
|
expires_at: string;
|
|
};
|
|
|
|
export type SessionHeartbeatResponse = {
|
|
status: "online";
|
|
last_seen_at: string;
|
|
client_ip: string | null;
|
|
};
|
|
|
|
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 const heartbeatSession = (token: string): Promise<AxiosResponse<SessionHeartbeatResponse>> =>
|
|
authClient.post<SessionHeartbeatResponse>(
|
|
"/api/v1/auth/session/heartbeat",
|
|
{},
|
|
{ headers: { Authorization: `Bearer ${token}` }, timeout: 5000 },
|
|
);
|
|
|
|
export const logoutSession = (token: string): Promise<AxiosResponse<void>> =>
|
|
authClient.post<void>(
|
|
"/api/v1/auth/session/logout",
|
|
{},
|
|
{ headers: { Authorization: `Bearer ${token}` } },
|
|
);
|
|
|
|
export default authClient;
|