Skip to content

Commit a56a226

Browse files
committed
feat: add putJson method to AuthProvider interface and implementations
1 parent 7b1cff0 commit a56a226

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

apps/client/src/authentication/providers/auth-provider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export interface AuthProvider {
6363
// API interaction helpers
6464
getJson(url: string): Promise<any>;
6565
postJson(url: string, data: any): Promise<any>;
66+
putJson(url: string, data: any): Promise<any>;
6667
deleteJson(url: string): Promise<any>;
6768
}
6869

apps/client/src/authentication/providers/auth0-provider.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,27 @@ export const useAuth0Provider = (): AuthProvider => {
171171
}
172172
};
173173

174+
const putJson = async (url: string, data: any): Promise<any> => {
175+
try {
176+
const accessToken = await getAccessToken();
177+
178+
const apiResponse = await fetch(url, {
179+
method: "PUT",
180+
headers: {
181+
Authorization: `Bearer ${accessToken}`,
182+
"Content-Type": "application/json",
183+
},
184+
body: JSON.stringify(data),
185+
});
186+
187+
return await apiResponse.json();
188+
} catch (error) {
189+
// eslint-disable-next-line no-console
190+
console.error("Error putting JSON:", error);
191+
throw error;
192+
}
193+
};
194+
174195
return {
175196
isAuthenticated,
176197
isLoading,
@@ -181,6 +202,7 @@ export const useAuth0Provider = (): AuthProvider => {
181202
hasPermission,
182203
getJson,
183204
postJson,
205+
putJson,
184206
deleteJson,
185207
};
186208
};

apps/client/src/authentication/providers/dex-provider.tsx

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ export const useDexProvider = (
193193
return () => {
194194
userManager.events.removeUserLoaded(addUserSignedIn);
195195
userManager.events.removeUserUnloaded(addUserSignedOut);
196-
userManager.events.removeAccessTokenExpiring(() => {});
197-
userManager.events.removeAccessTokenExpired(() => {});
196+
userManager.events.removeAccessTokenExpiring(() => { });
197+
userManager.events.removeAccessTokenExpired(() => { });
198198
};
199199
}, [userManager]);
200200

@@ -425,14 +425,44 @@ export const useDexProvider = (
425425
}
426426
};
427427

428+
const putJson = async (url: string, data: any): Promise<any> => {
429+
try {
430+
const accessToken = await getAccessToken();
431+
432+
if (!accessToken) {
433+
throw new Error("Not authenticated");
434+
}
435+
436+
const response = await fetch(url, {
437+
method: "PUT",
438+
headers: {
439+
Authorization: `Bearer ${accessToken}`,
440+
"Content-Type": "application/json",
441+
},
442+
body: JSON.stringify(data),
443+
});
444+
445+
if (!response.ok) {
446+
throw new Error(
447+
`HTTP error ${response.status}: ${response.statusText}`,
448+
);
449+
}
450+
451+
return await response.json();
452+
} catch (error) {
453+
console.error("Error putting JSON:", error);
454+
throw error;
455+
}
456+
};
457+
428458
// Map OIDC user to common AuthUser format
429459
const authUser: AuthUser | null = user
430460
? {
431-
name: user.profile.name,
432-
nickname: user.profile.nickname || user.profile.preferred_username,
433-
email: user.profile.email,
434-
...user.profile,
435-
}
461+
name: user.profile.name,
462+
nickname: user.profile.nickname || user.profile.preferred_username,
463+
email: user.profile.email,
464+
...user.profile,
465+
}
436466
: null;
437467

438468
return {
@@ -445,6 +475,7 @@ export const useDexProvider = (
445475
hasPermission,
446476
getJson,
447477
postJson,
478+
putJson,
448479
deleteJson,
449480
};
450481
};

0 commit comments

Comments
 (0)