Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@mui/lab": "6.0.0-beta.22",
"@mui/material": "<7.0.0",
"@mui/x-date-pickers": "^7.17.0",
"keycloak-js": "^26.2.0",
"react": "^19.1.0",
"react-dom": "^19.1.0",
"react-router-dom": "^7.7.1"
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 57 additions & 6 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
import Keycloak from "keycloak-js";

const initOptions = {
url: "https://authn.diamond.ac.uk",
realm: "master",
clientId: "visr-app",
};

const keycloak = new Keycloak(initOptions);

keycloak.onTokenExpired = () => {
keycloak
.updateToken(10)
.then(refreshed => {
if (refreshed) {
console.log("Token refreshed");
} else {
console.log("Token still valid");
}
})
.catch(err => {
console.error("Token refresh failed", err);
});
};

const kcInit = keycloak
.init({ onLoad: "login-required" })
.then(authenticated => {
if (authenticated) {
console.log("Authenticated");
} else {
console.error("Not authenticated");
}
});

export interface Plan {
name: string;
description: string | undefined;
Expand All @@ -19,18 +54,27 @@ export interface TaskRequest {
}

export async function getPlans(): Promise<PlansResponse> {
const url = "/api/plans";
if (!keycloak.authenticated) {
await kcInit;
}

const headers = new Headers();
headers.append("Content-Type", "application/json");
headers.append("X-Requested-By", "XMLHttpRequest");
if (!keycloak.token) {
throw new Error("No Keycloak token available");
}
const url = "/api/plans";
console.log("getting plans");

const response = await fetch(url, {
method: "GET",
headers: headers,
headers: {
Authorization: `Bearer ${keycloak.token}`,
"Content-Type": "application/json",
Accept: "application/json",
},
});

return await response.json();
const result = await response.json();
return result;
}

export async function createAndStartTask(
Expand All @@ -53,6 +97,13 @@ export async function createTask(request: TaskRequest): Promise<TaskResponse> {
body: JSON.stringify(request),
});

if (!response.ok) {
const errorText = await response.text();
throw new Error(
`Error ${response.status}: ${response.statusText}\n${errorText}`,
);
}

return await response.json();
}

Expand Down