Skip to content

Commit 00a554f

Browse files
committed
fix: error handling improvements and improved security
1 parent d033a0d commit 00a554f

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

lib/sessionManager/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export function splitString(str: string, length: number): string[] {
22
if (length <= 0) {
33
return [];
44
}
5-
return str.match(new RegExp(`.{1,${length}}`, 'g')) || [];
5+
return str.match(new RegExp(`.{1,${length}}`, "g")) || [];
66
}

lib/utils/exchangeAuthCode.test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ describe("exhangeAuthCode", () => {
192192
});
193193
});
194194

195-
196195
it("set the framework and version on header", async () => {
197196
const store = new MemoryStorage();
198197
setActiveStorage(store);
@@ -226,6 +225,5 @@ describe("exhangeAuthCode", () => {
226225
success: false,
227226
error: "Token exchange failed: 500 - error",
228227
});
229-
230228
});
231229
});

lib/utils/exchangeAuthCode.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ export const exchangeAuthCode = async ({
4242

4343
const activeStorage = getActiveStorage();
4444
if (!activeStorage) {
45-
throw new Error("No active storage found");
45+
console.error("No active storage found");
46+
return {
47+
success: false,
48+
error: `Authentication storage is not initialized`,
49+
};
4650
}
4751

4852
// warn if framework and version has not been set
@@ -65,8 +69,15 @@ export const exchangeAuthCode = async ({
6569
StorageKeys.codeVerifier,
6670
)) as string;
6771

68-
const headers: { "Content-type": string; "Kinde-SDK"?: string } = {
72+
const headers: {
73+
"Content-type": string;
74+
"Cache-Control": string;
75+
Pragma: string;
76+
"Kinde-SDK"?: string;
77+
} = {
6978
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
79+
"Cache-Control": "no-store",
80+
Pragma: "no-cache",
7081
};
7182

7283
if (frameworkSettings.framework) {
@@ -89,7 +100,7 @@ export const exchangeAuthCode = async ({
89100
});
90101
if (!response?.ok) {
91102
const errorText = await response.text();
92-
console.error('Token exchange failed:', response.status, errorText);
103+
console.error("Token exchange failed:", response.status, errorText);
93104
return {
94105
success: false,
95106
error: `Token exchange failed: ${response.status} - ${errorText}`,
@@ -111,9 +122,14 @@ export const exchangeAuthCode = async ({
111122
await activeStorage.removeItems(StorageKeys.state, StorageKeys.codeVerifier);
112123

113124
// Clear all url params
114-
const url = new URL(window.location.toString());
115-
url.search = "";
116-
window.history.pushState({}, "", url);
125+
const cleanUrl = (url: URL): URL => {
126+
url.search = "";
127+
url.hash = "";
128+
return url;
129+
};
130+
const url = cleanUrl(new URL(window.location.toString()));
131+
// Replace current state and clear forward history
132+
window.history.replaceState(null, "", url);
117133

118134
return {
119135
success: true,

0 commit comments

Comments
 (0)