/** * Auth API client for the DeutschLernen frontend * Handles authentication requests to the backend */ import type { RegisterRequest, LoginRequest, AuthResponse, CurrentUser, RefreshTokenRequest, RefreshTokenResponse, } from '@/types/api/auth'; // Base API URL - uses /api prefix for Docker proxy const BASE_URL = import.meta.env.PROD ? '/api' : import.meta.env.VITE_API_URL || 'http://localhost:5000/api'; /** * Register a new user */ export async function register(request: RegisterRequest): Promise { const response = await fetch(`${BASE_URL}/auth/register`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(request), }); if (!response.ok) { const error = await response.text(); throw new Error(error || 'Registration failed'); } return response.json(); } /** * Login an existing user */ export async function login(request: LoginRequest): Promise { const response = await fetch(`${BASE_URL}/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(request), }); if (!response.ok) { const error = await response.text(); throw new Error(error || 'Login failed'); } return response.json(); } /** * Get current authenticated user info */ export async function getCurrentUser(token: string): Promise { const response = await fetch(`${BASE_URL}/auth/me`, { method: 'GET', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, }, }); if (!response.ok) { const error = await response.text(); throw new Error(error || 'Failed to get current user'); } return response.json(); } /** * Refresh access token using refresh token */ export async function refreshToken(request: RefreshTokenRequest): Promise { const response = await fetch(`${BASE_URL}/auth/refresh`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(request.refreshToken), }); if (!response.ok) { const error = await response.text(); throw new Error(error || 'Token refresh failed'); } return response.json(); } /** * Revoke refresh token */ export async function revokeRefreshToken(refreshToken: string, accessToken: string): Promise { const response = await fetch(`${BASE_URL}/auth/revoke-refresh`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${accessToken}`, }, body: JSON.stringify(refreshToken), }); if (!response.ok) { const error = await response.text(); throw new Error(error || 'Failed to revoke refresh token'); } } /** * Check if an admin user exists (for bootstrap) */ export async function checkAdminExists(): Promise { const response = await fetch(`${BASE_URL}/bootstrap/admin-exists`, { method: 'GET', }); if (!response.ok) { return false; } return response.json(); } /** * Create first admin user (bootstrap endpoint) */ export async function createAdminUser(request: RegisterRequest): Promise { const response = await fetch(`${BASE_URL}/bootstrap/admin`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(request), }); if (!response.ok) { const error = await response.text(); throw new Error(error || 'Failed to create admin user'); } return response.json(); }