-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuserService.ts
69 lines (53 loc) · 1.76 KB
/
userService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import axios from 'axios';
class UserService {
getUser(userId: number) {
return axios.get(`/users/${userId}`);
}
async updateUser(userId: number, userDetails: any) {
try {
const token = localStorage.getItem('token');
if (!token) {
throw new Error('No token found');
}
const response = await axios.put(`/users/${userId}`, userDetails, {
headers: {
'Authorization': `Bearer ${token}`
}
});
return response;
} catch (error) {
console.error('Error updating user:', error);
throw error;
}
}
async deleteUser(userId: number) {
try {
const token = localStorage.getItem('token');
if (!token) {
throw new Error('No token found');
}
const response = await axios.delete(`/users/${userId}`, {
headers: {
'Authorization': `Bearer ${token}`
}
});
return response;
} catch (error) {
console.error('Error deleting user:', error);
throw error;
}
}
async updatePassword(userId: any, newPassword: any) {
return this.updateUser(userId, { password: newPassword });
}
async updateEmail(userId: any, newEmail: any) {
return this.updateUser(userId, { email: newEmail });
}
async updateFirstName(userId: any, newFirstName: any) {
return this.updateUser(userId, { firstname: newFirstName });
}
async updateLastName(userId: any, newLastName: any) {
return this.updateUser(userId, { lastname: newLastName });
}
}
export default new UserService();