Skip to content

Commit 6f4968d

Browse files
committed
Merge branch 'all-agent-list' into web-app
2 parents 62793a9 + abc5c9b commit 6f4968d

File tree

6 files changed

+184
-309
lines changed

6 files changed

+184
-309
lines changed

web/src/lib/components/CreateAgentDialog.svelte

+1-55
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import { validateURL } from "$lib/utils/validateURL";
1010
import IconInternet from "$lib/components/icons/IconInternet.svelte";
1111
import IconClose from "~icons/carbon/close";
12+
import { apiRequest } from "$lib/utils/authenticate";
1213
1314
export let showModal: boolean;
1415
export let onClose: () => void;
@@ -117,61 +118,6 @@
117118
}
118119
}
119120
120-
async function apiRequest(url: string, options: RequestInit) {
121-
try {
122-
const response = await fetch(url, {
123-
...options,
124-
headers: {
125-
...options.headers,
126-
},
127-
});
128-
129-
if (response.status === 401) {
130-
const refreshed = await refreshAccessToken();
131-
if (refreshed) {
132-
// Retry the request with the refreshed token
133-
options.headers = {
134-
...options.headers,
135-
Authorization: `Bearer ${accessToken}`, // use updated token
136-
};
137-
return await fetch(url, options);
138-
}
139-
}
140-
141-
return response;
142-
} catch (error) {
143-
throw error;
144-
}
145-
}
146-
147-
async function refreshAccessToken() {
148-
try {
149-
const refreshToken = localStorage.getItem("refreshToken");
150-
const response = await fetch(
151-
"https://api.commanddash.dev/account/github/refresh",
152-
{
153-
method: "POST",
154-
headers: {
155-
Authorization: `Bearer ${refreshToken}`,
156-
},
157-
}
158-
);
159-
const _response = await response.json();
160-
if (response.ok) {
161-
accessToken = _response.access_token;
162-
if (!!accessToken) {
163-
localStorage.setItem("accessToken", accessToken);
164-
}
165-
return true;
166-
} else {
167-
console.error("Failed to refresh token");
168-
return false;
169-
}
170-
} catch (error) {
171-
console.error("refreshAccessToken: error", error);
172-
return false;
173-
}
174-
}
175121
176122
function adjustGithubPermissions() {
177123
openPopup(

web/src/lib/components/PrivateAgentDialog.svelte

+1-59
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
import { toastStore } from "$lib/stores/ToastStores";
66
77
import CarbonGithub from "~icons/carbon/logo-github";
8-
import CarbonUpload from "~icons/carbon/upload";
9-
import CarbonPen from "~icons/carbon/pen";
108
import CarbonCode from "~icons/carbon/code";
119
import CarbonWorld from "~icons/carbon/wikis";
1210
1311
import IconInternet from "./icons/IconInternet.svelte";
1412
import { ToastType } from "$lib/types/Toast";
13+
import { apiRequest } from "$lib/utils/authenticate";
1514
import { goto } from "$app/navigation";
1615
1716
type ActionData = {
@@ -93,63 +92,6 @@
9392
}
9493
}
9594
96-
async function apiRequest(url: string, options: RequestInit) {
97-
try {
98-
const response = await fetch(url, {
99-
...options,
100-
headers: {
101-
...options.headers,
102-
Authorization: "Bearer " + accessToken,
103-
},
104-
});
105-
106-
if (response.status === 401) {
107-
const refreshed = await refreshAccessToken();
108-
if (refreshed) {
109-
// Retry the request with the refreshed token
110-
options.headers = {
111-
...options.headers,
112-
Authorization: `Bearer ${accessToken}`, // use updated token
113-
};
114-
return await fetch(url, options);
115-
}
116-
}
117-
118-
return response;
119-
} catch (error) {
120-
throw error;
121-
}
122-
}
123-
124-
async function refreshAccessToken() {
125-
try {
126-
const refreshToken = localStorage.getItem("refreshToken");
127-
const response = await fetch(
128-
"https://api.commanddash.dev/account/github/refresh",
129-
{
130-
method: "POST",
131-
headers: {
132-
Authorization: `Bearer ${refreshToken}`,
133-
},
134-
}
135-
);
136-
const _response = await response.json();
137-
if (response.ok) {
138-
accessToken = _response.access_token;
139-
if (!!accessToken) {
140-
localStorage.setItem("accessToken", accessToken);
141-
}
142-
return true;
143-
} else {
144-
console.error("Failed to refresh token");
145-
return false;
146-
}
147-
} catch (error) {
148-
console.error("refreshAccessToken: error", error);
149-
return false;
150-
}
151-
}
152-
15395
async function validatingRepositoryAccess(_url: string) {
15496
const headers = {
15597
"Content-Type": "application/json",

web/src/lib/utils/authenticate.ts

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
type RequestOptions = Omit<RequestInit, "headers"> & {
2+
headers?: Record<string, string>;
3+
};
4+
5+
export async function apiRequest(url: string, options: RequestOptions = {}) {
6+
try {
7+
let accessToken = localStorage.getItem("accessToken");
8+
const headers: Record<string, string> = {
9+
...options.headers,
10+
"Content-Type": "application/json",
11+
};
12+
13+
if (accessToken) {
14+
headers.Authorization = `Bearer ${accessToken}`;
15+
}
16+
17+
const response = await fetch(url, {
18+
...options,
19+
headers,
20+
});
21+
22+
// Check if access token has expired (401 Unauthorized)
23+
if (response.status === 401) {
24+
const refreshed = await refreshAccessToken();
25+
if (refreshed) {
26+
// Retry the request with the new access token
27+
accessToken = localStorage.getItem("accessToken");
28+
headers.Authorization = `Bearer ${accessToken}`;
29+
return await fetch(url, {
30+
...options,
31+
headers,
32+
});
33+
}
34+
}
35+
36+
return response;
37+
} catch (error) {
38+
throw new Error(`API request failed: ${error}`);
39+
}
40+
}
41+
42+
export async function refreshAccessToken(): Promise<boolean> {
43+
try {
44+
const refreshToken = localStorage.getItem("refreshToken");
45+
if (!refreshToken) {
46+
console.error("No refresh token available");
47+
return false;
48+
}
49+
50+
const response = await fetch("https://api.commanddash.dev/account/github/refresh", {
51+
method: "POST",
52+
headers: {
53+
Authorization: `Bearer ${refreshToken}`,
54+
},
55+
});
56+
57+
const data = await response.json();
58+
59+
if (response.ok) {
60+
const newAccessToken = data.access_token;
61+
if (newAccessToken) {
62+
localStorage.setItem("accessToken", newAccessToken);
63+
return true;
64+
}
65+
} else {
66+
console.error("Failed to refresh token", data);
67+
}
68+
69+
return false;
70+
} catch (error) {
71+
console.error("Error refreshing access token:", error);
72+
return false;
73+
}
74+
}

0 commit comments

Comments
 (0)