Skip to content

Commit

Permalink
made search dir
Browse files Browse the repository at this point in the history
  • Loading branch information
sergesoroka committed Oct 8, 2024
1 parent b665c46 commit f27c411
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions public/search/serviceWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
let accessToken = "";

self.addEventListener("install", (event) => {
self.skipWaiting();
console.log("Service Worker is installed");
});

self.addEventListener("activate", (event) => {
event.waitUntil(self.clients.claim());
console.log("ServiceWorker.js activated");
});

self.addEventListener("message", (event) => {
if (event.data && event.data.type === "TOKEN") {
accessToken = event.data.token;
console.log(
"Message Event: token received by Service Worker:",
accessToken
);
}
});

self.addEventListener("fetch", (event) => {
const { request } = event;
const url = new URL(event.request.url);
console.log("fetch", url);

if (
accessToken &&
url.origin.startsWith("https://") &&
url.origin.endsWith(".ideaconsult.net") &&
request.method === "GET" &&
url.origin !== "https://iam.ideaconsult.net" &&
url.origin !== "https://idp.ideaconsult.net" &&
request.destination === "image" &&
event.request.headers["Authorization"] == undefined
) {
const authRequest = new Request(request, {
headers: new Headers({
...request.headers,
Authorization: `Bearer ${accessToken}`,
}),
mode: "cors",
});
event.respondWith(fetch(authRequest));
console.log("with token");
} else {
// event.respondWith(fetch(request));
console.log("without token");
}
});

0 comments on commit f27c411

Please sign in to comment.