Files
Messenger/web/src/api/auth.ts
Codex 776a7634d2
Some checks failed
Android CI / android (push) Has started running
Android Release / release (push) Has been cancelled
CI / test (push) Has been cancelled
web: fix reset password token flow and auth interceptor
2026-03-09 21:52:24 +03:00

100 lines
3.1 KiB
TypeScript

import { http } from "./http";
import type { AuthSession, AuthUser, TokenPair } from "../chat/types";
export async function registerRequest(email: string, name: string, username: string, password: string): Promise<void> {
await http.post("/auth/register", { email, name, username, password });
}
export async function verifyEmailRequest(token: string): Promise<void> {
await http.post("/auth/verify-email", { token });
}
export async function requestPasswordResetRequest(email: string): Promise<void> {
await http.post("/auth/request-password-reset", { email });
}
export async function resetPasswordRequest(token: string, password: string): Promise<void> {
await http.post("/auth/reset-password", { token, new_password: password });
}
export async function loginRequest(email: string, password: string, otpCode?: string, recoveryCode?: string): Promise<TokenPair> {
const { data } = await http.post<TokenPair>("/auth/login", {
email,
password,
otp_code: otpCode || undefined,
recovery_code: recoveryCode || undefined,
});
return data;
}
export async function refreshRequest(refreshToken: string): Promise<TokenPair> {
const { data } = await http.post<TokenPair>("/auth/refresh", { refresh_token: refreshToken });
return data;
}
export async function meRequest(): Promise<AuthUser> {
const { data } = await http.get<AuthUser>("/auth/me");
return data;
}
export async function listSessions(): Promise<AuthSession[]> {
const { data } = await http.get<AuthSession[]>("/auth/sessions");
return data;
}
export async function revokeSession(jti: string): Promise<void> {
await http.delete(`/auth/sessions/${jti}`);
}
export async function revokeAllSessions(): Promise<void> {
await http.delete("/auth/sessions");
}
export interface EmailStatusResponse {
email: string;
registered: boolean;
email_verified: boolean;
twofa_enabled: boolean;
}
export async function checkEmailStatus(email: string): Promise<EmailStatusResponse> {
const { data } = await http.get<EmailStatusResponse>("/auth/check-email", { params: { email } });
return data;
}
export interface TwoFactorSetupResponse {
secret: string;
otpauth_url: string;
}
export async function setupTwoFactor(): Promise<TwoFactorSetupResponse> {
const { data } = await http.post<TwoFactorSetupResponse>("/auth/2fa/setup");
return data;
}
export async function enableTwoFactor(code: string): Promise<void> {
await http.post("/auth/2fa/enable", { code });
}
export async function disableTwoFactor(code: string): Promise<void> {
await http.post("/auth/2fa/disable", { code });
}
export interface TwoFactorRecoveryStatusResponse {
remaining_codes: number;
}
export interface TwoFactorRecoveryCodesResponse {
codes: string[];
}
export async function getTwoFactorRecoveryStatus(): Promise<TwoFactorRecoveryStatusResponse> {
const { data } = await http.get<TwoFactorRecoveryStatusResponse>("/auth/2fa/recovery-codes/status");
return data;
}
export async function regenerateTwoFactorRecoveryCodes(code: string): Promise<TwoFactorRecoveryCodesResponse> {
const { data } = await http.post<TwoFactorRecoveryCodesResponse>("/auth/2fa/recovery-codes/regenerate", { code });
return data;
}