|
| 1 | +export interface User { |
| 2 | + id: string; |
| 3 | + username: string; |
| 4 | + email: string; |
| 5 | + isAdmin: boolean; |
| 6 | + createdAt?: string; |
| 7 | +} |
| 8 | + |
| 9 | +export interface UpdateUserRequestType { |
| 10 | + username?: string; |
| 11 | + password?: string; |
| 12 | + email?: string; |
| 13 | +} |
| 14 | + |
| 15 | +export interface UpdateUserResponseType { |
| 16 | + message: string; |
| 17 | + data: User; |
| 18 | +} |
| 19 | + |
| 20 | +export interface VerifyTokenResponseType { |
| 21 | + message: string; |
| 22 | + data: User; |
| 23 | +} |
| 24 | + |
| 25 | +export const UpdateUser = async ( |
| 26 | + user: UpdateUserRequestType, |
| 27 | + id: string |
| 28 | +): Promise<UpdateUserResponseType> => { |
| 29 | + const JWT_TOKEN = localStorage.getItem("TOKEN") ?? undefined; |
| 30 | + const response = await fetch( |
| 31 | + `${process.env.NEXT_PUBLIC_USER_SERVICE_URL}users/${id}`, |
| 32 | + { |
| 33 | + method: "PATCH", |
| 34 | + headers: { |
| 35 | + "Content-Type": "application/json", |
| 36 | + Authorization: `Bearer ${JWT_TOKEN}`, |
| 37 | + }, |
| 38 | + body: JSON.stringify(user), |
| 39 | + } |
| 40 | + ); |
| 41 | + |
| 42 | + if (response.status === 200) { |
| 43 | + return response.json(); |
| 44 | + } else { |
| 45 | + throw new Error( |
| 46 | + `Error updating user: ${response.status} ${response.statusText}` |
| 47 | + ); |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +export const ValidateUser = async (): Promise<VerifyTokenResponseType> => { |
| 52 | + const JWT_TOKEN = localStorage.getItem("TOKEN") ?? undefined; |
| 53 | + const response = await fetch( |
| 54 | + `${process.env.NEXT_PUBLIC_USER_SERVICE_URL}auth/verify-token`, |
| 55 | + { |
| 56 | + method: "GET", |
| 57 | + headers: { |
| 58 | + "Content-Type": "application/json", |
| 59 | + Authorization: `Bearer ${JWT_TOKEN}`, |
| 60 | + }, |
| 61 | + } |
| 62 | + ); |
| 63 | + |
| 64 | + if (response.status === 200) { |
| 65 | + return response.json(); |
| 66 | + } else { |
| 67 | + throw new Error( |
| 68 | + `Error verifying token: ${response.status} ${response.statusText}` |
| 69 | + ); |
| 70 | + } |
| 71 | +}; |
0 commit comments