feat(auth): implement login authorization
feat(font): implement satoshi font family
This commit is contained in:
101
src/utils/api.ts
Normal file
101
src/utils/api.ts
Normal 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);
|
||||
Reference in New Issue
Block a user