authService.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { API_BASE_URL } from '../utils/constants';
  2. import { apiClient } from './apiClient';
  3. interface AuthResponse {
  4. access_token: string;
  5. }
  6. export const authService = {
  7. async login(username: string, password: string): Promise<AuthResponse> {
  8. const response = await fetch(`${API_BASE_URL}/auth/login`, {
  9. method: 'POST',
  10. headers: {
  11. 'Content-Type': 'application/json',
  12. },
  13. body: JSON.stringify({ username, password }),
  14. });
  15. if (!response.ok) {
  16. const errorData = await response.json();
  17. throw new Error(errorData.message || 'Login failed');
  18. }
  19. return response.json();
  20. },
  21. async getProfile(token: string): Promise<any> {
  22. const response = await apiClient.request('/auth/profile', {
  23. method: 'GET',
  24. headers: {
  25. 'Content-Type': 'application/json',
  26. },
  27. });
  28. if (!response.ok) {
  29. const errorData = await response.json();
  30. throw new Error(errorData.message || 'Failed to fetch profile');
  31. }
  32. return response.json();
  33. },
  34. };