feat(auth): implement login authorization

feat(font): implement satoshi font family
This commit is contained in:
shafin-r
2026-01-11 15:44:51 +06:00
parent bd6f1d2333
commit 1506c25796
35 changed files with 3870 additions and 7 deletions

101
src/utils/api.ts Normal file
View File

@ -0,0 +1,101 @@
const API_URL = "https://dsat-api.edbridgescholars.com";
export interface LoginRequest {
email: string;
password: string;
}
export interface User {
email: string;
name: string;
role: "STUDENT" | "TEACHER" | "ADMIN";
avatar_url: string;
id: string;
status: "ACTIVE" | "INACTIVE";
joined_at: string;
last_active: string;
}
export interface LoginResponse {
token: string;
token_type: string;
user: User;
}
export interface ApiError {
detail?: string;
message?: string;
}
class ApiClient {
private baseURL: string;
constructor(baseURL: string) {
this.baseURL = baseURL;
}
private async request<T>(
endpoint: string,
options: RequestInit = {}
): Promise<T> {
const url = `${this.baseURL}${endpoint}`;
const config: RequestInit = {
...options,
headers: {
"Content-Type": "application/json",
...options.headers,
},
};
try {
const response = await fetch(url, config);
if (!response.ok) {
const error: ApiError = await response.json().catch(() => ({
message: "An error occurred",
}));
throw new Error(error.detail || error.message || "Request failed");
}
return await response.json();
} catch (error) {
if (error instanceof Error) {
throw error;
}
throw new Error("Network error occurred");
}
}
// Auth endpoints
async login(credentials: LoginRequest): Promise<LoginResponse> {
return this.request<LoginResponse>("/auth/login/", {
method: "POST",
body: JSON.stringify(credentials),
});
}
// Authenticated request helper
async authenticatedRequest<T>(
endpoint: string,
token: string,
options: RequestInit = {}
): Promise<T> {
return this.request<T>(endpoint, {
...options,
headers: {
...options.headers,
Authorization: `Bearer ${token}`,
},
});
}
// Example: Get user profile (authenticated endpoint)
async getUserProfile(token: string): Promise<User> {
return this.authenticatedRequest<User>("/auth/me", token);
}
// Add more API methods here as needed
}
export const api = new ApiClient(API_URL);