Files
badoHair_fe/lib/api/auth.ts

56 lines
1.5 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 | null> {
const res = await api.post<AuthResponse | { message: string }>("/auth/register", { email, password, name });
if ("access_token" in res) {
setTokens(res.access_token, (res as AuthResponse).refresh_token);
return api.get<UserProfile>("/auth/me");
}
// Email confirmation required — no session yet
return null;
}
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();
}