Update May 12 by Elvis

This commit is contained in:
belviskhoremk
2026-05-12 00:28:37 +00:00
parent b32a70cd0e
commit c4450c993b
37 changed files with 3749 additions and 600 deletions

53
lib/api/auth.ts Normal file
View File

@@ -0,0 +1,53 @@
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();
}