authService.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 language = localStorage.getItem('userLanguage') || 'ja';
  9. const response = await fetch(`${API_BASE_URL}/auth/login`, {
  10. method: 'POST',
  11. headers: {
  12. 'Content-Type': 'application/json',
  13. 'x-user-language': language,
  14. },
  15. body: JSON.stringify({ username, password }),
  16. });
  17. if (!response.ok) {
  18. const errorData = await response.json();
  19. throw new Error(errorData.message || 'Login failed');
  20. }
  21. return response.json();
  22. },
  23. async getProfile(token: string): Promise<any> {
  24. const response = await apiClient.request('/auth/profile', {
  25. method: 'GET',
  26. headers: {
  27. 'Content-Type': 'application/json',
  28. },
  29. });
  30. if (!response.ok) {
  31. const errorData = await response.json();
  32. throw new Error(errorData.message || 'Failed to fetch profile');
  33. }
  34. return response.json();
  35. },
  36. };