mirror of
http://88.130.71.182:3000/BlitTech/badoHair_fe.git
synced 2026-06-13 10:41:11 +00:00
Update May 12 by Elvis
This commit is contained in:
53
lib/api/auth.ts
Normal file
53
lib/api/auth.ts
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user