Files
badoHair_fe/lib/api/auth.ts
2026-05-12 00:28:37 +00:00

54 lines
1.4 KiB
TypeScript

import { api, setTokens, clearTokens } from "@/lib/api";
export interface UserProfile {
id: string;
email: string;
full_name: string | null;
phone: string | null;
role: "client" | "admin";
}
interface AuthResponse {
access_token: string;
refresh_token: string;
token_type: string;
expires_in: number;
}
export async function login(email: string, password: string): Promise<UserProfile> {
const tokens = await api.post<AuthResponse>("/auth/login", { email, password });
setTokens(tokens.access_token, tokens.refresh_token);
return api.get<UserProfile>("/auth/me");
}
export async function register(
email: string,
password: string,
name: string
): Promise<UserProfile> {
const tokens = await api.post<AuthResponse>("/auth/register", { email, password, name });
if ("access_token" in (tokens as object)) {
setTokens((tokens as AuthResponse).access_token, (tokens as AuthResponse).refresh_token);
}
return api.get<UserProfile>("/auth/me");
}
export async function getMe(): Promise<UserProfile> {
return api.get<UserProfile>("/auth/me");
}
export async function updateProfile(
full_name: string,
phone: string | null
): Promise<UserProfile> {
return api.patch<UserProfile>("/auth/me", { full_name, phone });
}
export async function forgotPassword(email: string): Promise<void> {
await api.post("/auth/forgot-password", { email });
}
export function logout() {
clearTokens();
}