| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- const API_BASE_URL = '/api';
- export const userService = {
- async changePassword(currentPassword: string, newPassword: string): Promise<{ message: string }> {
- const token = localStorage.getItem('authToken') || localStorage.getItem('token');
- const response = await fetch(`${API_BASE_URL}/users/password`, {
- method: 'PUT',
- headers: {
- 'Content-Type': 'application/json',
- 'Authorization': `Bearer ${token}`,
- },
- body: JSON.stringify({
- currentPassword,
- newPassword,
- }),
- });
- if (!response.ok) {
- const error = await response.json();
- throw new Error(error.message || 'パスワードの変更に失敗しました');
- }
- return response.json();
- },
- async getUsers(): Promise<any[]> {
- const token = localStorage.getItem('authToken') || localStorage.getItem('token');
- const response = await fetch(`${API_BASE_URL}/users`, {
- method: 'GET',
- headers: {
- 'Authorization': `Bearer ${token}`,
- },
- });
- if (!response.ok) {
- const error = await response.json();
- throw new Error(error.message || 'ユーザーリストの取得に失敗しました');
- }
- return response.json();
- },
- async updateUser(userId: string, isAdmin: boolean): Promise<{ message: string }> {
- const token = localStorage.getItem('authToken') || localStorage.getItem('token');
- const response = await fetch(`${API_BASE_URL}/users/${userId}`, {
- method: 'PUT',
- headers: {
- 'Content-Type': 'application/json',
- 'Authorization': `Bearer ${token}`,
- },
- body: JSON.stringify({
- isAdmin,
- }),
- });
- if (!response.ok) {
- const error = await response.json();
- throw new Error(error.message || 'ユーザー情報の更新に失敗しました');
- }
- return response.json();
- },
- async updateUserInfo(userId: string, userData: { isAdmin?: boolean; password?: string }): Promise<{ message: string }> {
- const token = localStorage.getItem('authToken') || localStorage.getItem('token');
- const response = await fetch(`${API_BASE_URL}/users/${userId}`, {
- method: 'PUT',
- headers: {
- 'Content-Type': 'application/json',
- 'Authorization': `Bearer ${token}`,
- },
- body: JSON.stringify(userData),
- });
- if (!response.ok) {
- const error = await response.json();
- throw new Error(error.message || 'ユーザー情報の更新に失敗しました');
- }
- return response.json();
- },
- async deleteUser(userId: string): Promise<{ message: string }> {
- const token = localStorage.getItem('authToken') || localStorage.getItem('token');
- const response = await fetch(`${API_BASE_URL}/users/${userId}`, {
- method: 'DELETE',
- headers: {
- 'Authorization': `Bearer ${token}`,
- },
- });
- if (!response.ok) {
- const error = await response.json();
- throw new Error(error.message || 'ユーザーの削除に失敗しました');
- }
- return response.json();
- },
- async createUser(username: string, password: string, isAdmin: boolean = false): Promise<{ message: string }> {
- const token = localStorage.getItem('authToken') || localStorage.getItem('token');
- const response = await fetch(`${API_BASE_URL}/users`, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'Authorization': `Bearer ${token}`,
- },
- body: JSON.stringify({
- username,
- password,
- isAdmin,
- }),
- });
- if (!response.ok) {
- const error = await response.json();
- throw new Error(error.message || 'ユーザーの作成に失敗しました');
- }
- return response.json();
- },
- };
|