120 行
3.3 KiB
TypeScript
120 行
3.3 KiB
TypeScript
import { apiRequestData } from "./http";
|
|
import { getDeviceFingerprint } from "../auth/deviceFingerprint";
|
|
import type { LoginResponse } from "./types";
|
|
|
|
const JSON_HEADERS = {
|
|
"content-type": "application/json",
|
|
};
|
|
|
|
export async function changePassword(payload: { oldPassword: string; newPassword: string }) {
|
|
return apiRequestData<LoginResponse>("/api/auth/change-password", {
|
|
method: "POST",
|
|
headers: {
|
|
...JSON_HEADERS,
|
|
"x-device-fingerprint": getDeviceFingerprint(),
|
|
},
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export async function forgotPassword(payload: { email: string }) {
|
|
return apiRequestData<Record<string, never>>("/api/auth/forgot-password", {
|
|
method: "POST",
|
|
auth: false,
|
|
headers: JSON_HEADERS,
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export async function resetPassword(payload: { email: string; code: string; newPassword: string }) {
|
|
return apiRequestData<Record<string, never>>("/api/auth/reset-password", {
|
|
method: "POST",
|
|
auth: false,
|
|
headers: JSON_HEADERS,
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export async function requestEmailLoginCode(payload: { email: string }) {
|
|
return apiRequestData<Record<string, never>>("/api/auth/email-login/code", {
|
|
method: "POST",
|
|
auth: false,
|
|
headers: JSON_HEADERS,
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export async function verifyEmailLoginCode(payload: { email: string; code: string }) {
|
|
return apiRequestData<LoginResponse>("/api/auth/email-login/verify", {
|
|
method: "POST",
|
|
auth: false,
|
|
retryOn401: false,
|
|
headers: {
|
|
...JSON_HEADERS,
|
|
"x-device-fingerprint": getDeviceFingerprint(),
|
|
},
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export type AuthCapabilities = {
|
|
emailDelivery: {
|
|
ready: boolean;
|
|
statusReasons: string[];
|
|
};
|
|
emailLoginCode: {
|
|
enabled: boolean;
|
|
};
|
|
passwordReset: {
|
|
enabled: boolean;
|
|
};
|
|
};
|
|
|
|
export type SsoProviderInfo = { id: "oidc"; label: string; enabled: boolean; loginUrl: string };
|
|
|
|
export type WecomEntryConfig = {
|
|
enabled: boolean;
|
|
entryEnabled: boolean;
|
|
oauthEnabled: boolean;
|
|
corpId: string;
|
|
agentId: string;
|
|
loginUrl: string | null;
|
|
bindEnabled: boolean;
|
|
defaultReturnTo: string;
|
|
status: "ready" | "disabled" | "misconfigured";
|
|
statusReasons: string[];
|
|
};
|
|
|
|
export async function fetchSsoProviders() {
|
|
return apiRequestData<SsoProviderInfo[]>("/api/auth/sso/providers", { auth: false });
|
|
}
|
|
|
|
export async function fetchAuthCapabilities() {
|
|
return apiRequestData<AuthCapabilities>("/api/auth/capabilities", { auth: false });
|
|
}
|
|
|
|
export async function fetchWecomConfig() {
|
|
return apiRequestData<WecomEntryConfig>("/api/auth/wecom/config", { auth: false });
|
|
}
|
|
|
|
export async function startWecomBind(returnTo = "/settings/profile") {
|
|
return apiRequestData<{ loginUrl: string }>("/api/auth/wecom/bind", {
|
|
method: "POST",
|
|
body: JSON.stringify({ returnTo }),
|
|
});
|
|
}
|
|
|
|
export async function unbindWecom() {
|
|
return apiRequestData<{ ok: boolean }>("/api/auth/wecom/unbind", {
|
|
method: "POST",
|
|
body: JSON.stringify({}),
|
|
});
|
|
}
|
|
|
|
export async function updateNotificationPreferences(payload: { notificationPushEnabled: boolean }) {
|
|
return apiRequestData<{ notificationPushEnabled: boolean }>("/api/me/notification-preferences", {
|
|
method: "PATCH",
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|