initial commit

This commit is contained in:
2026-03-12 06:37:16 +08:00
commit c31ebd35e7
41 changed files with 6272 additions and 0 deletions

26
src/api/client.ts Normal file
View File

@@ -0,0 +1,26 @@
import { API_BASE_URL } from "../config/constants";
export const authFetch = async (endpoint: string, options: RequestInit = {}) => {
const token = localStorage.getItem("accessToken");
const isFormData = options.body instanceof FormData;
const headers: HeadersInit = {
...(!isFormData && { "Content-Type": "application/json" }),
...(token ? { Authorization: `Bearer ${token}` } : {}),
...options.headers,
};
const response = await fetch(`${API_BASE_URL}${endpoint}`, {
...options,
headers,
});
if (response.status === 401) {
localStorage.removeItem("accessToken");
localStorage.removeItem("refreshToken");
window.location.href = "/login";
}
return response;
};

80
src/api/users.ts Normal file
View File

@@ -0,0 +1,80 @@
import { authFetch } from './client';
// --- Auth Endpoints ---
export const loginWithPassword = async (mobile: string, password: string) => {
const response = await authFetch('/api/users/login/', {
method: 'POST',
body: JSON.stringify({ mobile, password })
});
if (!response.ok) throw new Error('Failed to login with password');
return response.json();
};
export const sendOtp = async (mobile: string, mode: string) => {
const response = await authFetch('/api/users/otp/send/', {
method: 'POST',
body: JSON.stringify({ mobile, mode })
});
if (!response.ok) throw new Error('Failed to send OTP');
return response.json();
};
export const loginWithOtp = async (mobile: string, otp: string) => {
const response = await authFetch('/api/users/otp/login/', {
method: 'POST',
body: JSON.stringify({ mobile, otp })
});
if (!response.ok) throw new Error('Failed to login with OTP');
return response.json();
};
export const logoutUser = async (refreshToken: string) => {
const response = await authFetch('/api/users/logout/', {
method: 'POST',
body: JSON.stringify({ refresh: refreshToken })
});
if (!response.ok) throw new Error("Logout failed");
return response.json();
};
// --- Profile Endpoints ---
export const getUserProfile = async () => {
const response = await authFetch('/api/users/me/', {
method: 'GET'
});
if (!response.ok) throw new Error('Failed to fetch profile');
return response.json();
};
export const updateUserProfile = async (data: Record<string, string>) => {
const response = await authFetch('/api/users/me/', {
method: 'PATCH',
body: JSON.stringify(data)
});
if (!response.ok) throw new Error('Failed to update profile');
return response.json();
};
export const updateProfilePicture = async (file: File) => {
const formData = new FormData();
formData.append('profile_picture', file);
const response = await authFetch('/api/users/profile/picture/', {
method: 'POST',
body: formData
});
if (!response.ok) throw new Error('Failed to update profile picture');
return response.json();
};
export const removeProfilePicture = async () => {
const formData = new FormData();
formData.append('profile_picture', '');
return authFetch(`/api/users/profile/picture/`, {
method: 'POST',
body: formData,
});
};