userService.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const API_BASE_URL = '/api';
  2. export const userService = {
  3. async changePassword(currentPassword: string, newPassword: string): Promise<{ message: string }> {
  4. const token = localStorage.getItem('authToken') || localStorage.getItem('token');
  5. const response = await fetch(`${API_BASE_URL}/users/password`, {
  6. method: 'PUT',
  7. headers: {
  8. 'Content-Type': 'application/json',
  9. 'Authorization': `Bearer ${token}`,
  10. },
  11. body: JSON.stringify({
  12. currentPassword,
  13. newPassword,
  14. }),
  15. });
  16. if (!response.ok) {
  17. const error = await response.json();
  18. throw new Error(error.message || 'パスワードの変更に失敗しました');
  19. }
  20. return response.json();
  21. },
  22. async getUsers(): Promise<any[]> {
  23. const token = localStorage.getItem('authToken') || localStorage.getItem('token');
  24. const response = await fetch(`${API_BASE_URL}/users`, {
  25. method: 'GET',
  26. headers: {
  27. 'Authorization': `Bearer ${token}`,
  28. },
  29. });
  30. if (!response.ok) {
  31. const error = await response.json();
  32. throw new Error(error.message || 'ユーザーリストの取得に失敗しました');
  33. }
  34. return response.json();
  35. },
  36. async updateUser(userId: string, isAdmin: boolean): Promise<{ message: string }> {
  37. const token = localStorage.getItem('authToken') || localStorage.getItem('token');
  38. const response = await fetch(`${API_BASE_URL}/users/${userId}`, {
  39. method: 'PUT',
  40. headers: {
  41. 'Content-Type': 'application/json',
  42. 'Authorization': `Bearer ${token}`,
  43. },
  44. body: JSON.stringify({
  45. isAdmin,
  46. }),
  47. });
  48. if (!response.ok) {
  49. const error = await response.json();
  50. throw new Error(error.message || 'ユーザー情報の更新に失敗しました');
  51. }
  52. return response.json();
  53. },
  54. async updateUserInfo(userId: string, userData: { isAdmin?: boolean; password?: string }): Promise<{ message: string }> {
  55. const token = localStorage.getItem('authToken') || localStorage.getItem('token');
  56. const response = await fetch(`${API_BASE_URL}/users/${userId}`, {
  57. method: 'PUT',
  58. headers: {
  59. 'Content-Type': 'application/json',
  60. 'Authorization': `Bearer ${token}`,
  61. },
  62. body: JSON.stringify(userData),
  63. });
  64. if (!response.ok) {
  65. const error = await response.json();
  66. throw new Error(error.message || 'ユーザー情報の更新に失敗しました');
  67. }
  68. return response.json();
  69. },
  70. async deleteUser(userId: string): Promise<{ message: string }> {
  71. const token = localStorage.getItem('authToken') || localStorage.getItem('token');
  72. const response = await fetch(`${API_BASE_URL}/users/${userId}`, {
  73. method: 'DELETE',
  74. headers: {
  75. 'Authorization': `Bearer ${token}`,
  76. },
  77. });
  78. if (!response.ok) {
  79. const error = await response.json();
  80. throw new Error(error.message || 'ユーザーの削除に失敗しました');
  81. }
  82. return response.json();
  83. },
  84. async createUser(username: string, password: string, isAdmin: boolean = false): Promise<{ message: string }> {
  85. const token = localStorage.getItem('authToken') || localStorage.getItem('token');
  86. const response = await fetch(`${API_BASE_URL}/users`, {
  87. method: 'POST',
  88. headers: {
  89. 'Content-Type': 'application/json',
  90. 'Authorization': `Bearer ${token}`,
  91. },
  92. body: JSON.stringify({
  93. username,
  94. password,
  95. isAdmin,
  96. }),
  97. });
  98. if (!response.ok) {
  99. const error = await response.json();
  100. throw new Error(error.message || 'ユーザーの作成に失敗しました');
  101. }
  102. return response.json();
  103. },
  104. };