Skip to content

Commit

Permalink
Merge branch 'all-agent-list' into web-app
Browse files Browse the repository at this point in the history
  • Loading branch information
samyakkkk committed Sep 26, 2024
2 parents 62793a9 + abc5c9b commit 6f4968d
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 309 deletions.
56 changes: 1 addition & 55 deletions web/src/lib/components/CreateAgentDialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { validateURL } from "$lib/utils/validateURL";
import IconInternet from "$lib/components/icons/IconInternet.svelte";
import IconClose from "~icons/carbon/close";
import { apiRequest } from "$lib/utils/authenticate";
export let showModal: boolean;
export let onClose: () => void;
Expand Down Expand Up @@ -117,61 +118,6 @@
}
}
async function apiRequest(url: string, options: RequestInit) {
try {
const response = await fetch(url, {
...options,
headers: {
...options.headers,
},
});
if (response.status === 401) {
const refreshed = await refreshAccessToken();
if (refreshed) {
// Retry the request with the refreshed token
options.headers = {
...options.headers,
Authorization: `Bearer ${accessToken}`, // use updated token
};
return await fetch(url, options);
}
}
return response;
} catch (error) {
throw error;
}
}
async function refreshAccessToken() {
try {
const refreshToken = localStorage.getItem("refreshToken");
const response = await fetch(
"https://api.commanddash.dev/account/github/refresh",
{
method: "POST",
headers: {
Authorization: `Bearer ${refreshToken}`,
},
}
);
const _response = await response.json();
if (response.ok) {
accessToken = _response.access_token;
if (!!accessToken) {
localStorage.setItem("accessToken", accessToken);
}
return true;
} else {
console.error("Failed to refresh token");
return false;
}
} catch (error) {
console.error("refreshAccessToken: error", error);
return false;
}
}
function adjustGithubPermissions() {
openPopup(
Expand Down
60 changes: 1 addition & 59 deletions web/src/lib/components/PrivateAgentDialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import { toastStore } from "$lib/stores/ToastStores";
import CarbonGithub from "~icons/carbon/logo-github";
import CarbonUpload from "~icons/carbon/upload";
import CarbonPen from "~icons/carbon/pen";
import CarbonCode from "~icons/carbon/code";
import CarbonWorld from "~icons/carbon/wikis";
import IconInternet from "./icons/IconInternet.svelte";
import { ToastType } from "$lib/types/Toast";
import { apiRequest } from "$lib/utils/authenticate";
import { goto } from "$app/navigation";
type ActionData = {
Expand Down Expand Up @@ -93,63 +92,6 @@
}
}
async function apiRequest(url: string, options: RequestInit) {
try {
const response = await fetch(url, {
...options,
headers: {
...options.headers,
Authorization: "Bearer " + accessToken,
},
});
if (response.status === 401) {
const refreshed = await refreshAccessToken();
if (refreshed) {
// Retry the request with the refreshed token
options.headers = {
...options.headers,
Authorization: `Bearer ${accessToken}`, // use updated token
};
return await fetch(url, options);
}
}
return response;
} catch (error) {
throw error;
}
}
async function refreshAccessToken() {
try {
const refreshToken = localStorage.getItem("refreshToken");
const response = await fetch(
"https://api.commanddash.dev/account/github/refresh",
{
method: "POST",
headers: {
Authorization: `Bearer ${refreshToken}`,
},
}
);
const _response = await response.json();
if (response.ok) {
accessToken = _response.access_token;
if (!!accessToken) {
localStorage.setItem("accessToken", accessToken);
}
return true;
} else {
console.error("Failed to refresh token");
return false;
}
} catch (error) {
console.error("refreshAccessToken: error", error);
return false;
}
}
async function validatingRepositoryAccess(_url: string) {
const headers = {
"Content-Type": "application/json",
Expand Down
74 changes: 74 additions & 0 deletions web/src/lib/utils/authenticate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
type RequestOptions = Omit<RequestInit, "headers"> & {
headers?: Record<string, string>;
};

export async function apiRequest(url: string, options: RequestOptions = {}) {
try {
let accessToken = localStorage.getItem("accessToken");
const headers: Record<string, string> = {
...options.headers,
"Content-Type": "application/json",
};

if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`;
}

const response = await fetch(url, {
...options,
headers,
});

// Check if access token has expired (401 Unauthorized)
if (response.status === 401) {
const refreshed = await refreshAccessToken();
if (refreshed) {
// Retry the request with the new access token
accessToken = localStorage.getItem("accessToken");
headers.Authorization = `Bearer ${accessToken}`;
return await fetch(url, {
...options,
headers,
});
}
}

return response;
} catch (error) {
throw new Error(`API request failed: ${error}`);
}
}

export async function refreshAccessToken(): Promise<boolean> {
try {
const refreshToken = localStorage.getItem("refreshToken");
if (!refreshToken) {
console.error("No refresh token available");
return false;
}

const response = await fetch("https://api.commanddash.dev/account/github/refresh", {
method: "POST",
headers: {
Authorization: `Bearer ${refreshToken}`,
},
});

const data = await response.json();

if (response.ok) {
const newAccessToken = data.access_token;
if (newAccessToken) {
localStorage.setItem("accessToken", newAccessToken);
return true;
}
} else {
console.error("Failed to refresh token", data);
}

return false;
} catch (error) {
console.error("Error refreshing access token:", error);
return false;
}
}
Loading

0 comments on commit 6f4968d

Please sign in to comment.