Skip to content

Commit

Permalink
chech if token is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
sergesoroka committed Oct 10, 2024
1 parent 506e6fa commit 0f751e0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 138 deletions.
14 changes: 0 additions & 14 deletions components/Header/Header.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { useNavigate } from "react-router-dom";

import { useKeycloak } from "@react-keycloak/web";
import { useEffect } from "react";

export default function Header() {
const { keycloak } = useKeycloak();
const navigate = useNavigate();

keycloak.onTokenExpired = () => {
console.log("Token expired, attempting to refresh...");
};

const username = keycloak.tokenParsed?.preferred_username
? keycloak.tokenParsed?.preferred_username
: localStorage.getItem("username");
Expand All @@ -22,15 +17,6 @@ export default function Header() {
localStorage.removeItem("token");
localStorage.removeItem("refreshToken");
};
setInterval(async () => {
try {
await keycloak.updateToken(5);
localStorage.setItem("token", keycloak.token);
console.log("Refreshed!");
} catch (error) {
console.log("Falied to refresh token..", error);
}
}, [30000]);

return (
<div className="logo">
Expand Down
7 changes: 0 additions & 7 deletions public/serviceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@ 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
);
}
});

Expand Down Expand Up @@ -44,7 +38,6 @@ self.addEventListener("fetch", (event) => {
event.respondWith(fetch(authRequest));
console.log("with token");
} else {
// event.respondWith(fetch(request));
console.log("without token");
}
});
66 changes: 0 additions & 66 deletions public/worker.js

This file was deleted.

33 changes: 23 additions & 10 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,7 @@ function App() {
let [domain, setDomain] = useState(null);

const { keycloak } = useKeycloak();
const stored_token = localStorage.getItem("token");
// const token = keycloak.token ? keycloak.token : stored_token;

// if (navigator.serviceWorker.controller) {
// console.log("post message", navigator.serviceWorker);

// navigator.serviceWorker.controller.postMessage({
// type: "SET_TOKEN",
// token: token,
// });
// }
useEffect(() => {
if (keycloak.authenticated) {
localStorage.setItem("refreshToken", keycloak.refreshToken);
Expand All @@ -41,6 +31,29 @@ function App() {
}
}, [keycloak.authenticated]);

useEffect(() => {
const interval = setInterval(() => {
keycloak
.updateToken(30)
.then((refreshed) => {
if (refreshed) {
console.log("app: Token refreshed and updated in localStorage.");
localStorage.setItem("token", keycloak.token);
} else {
console.log("app: Token is still valid.");
}
})
.catch(() => {
localStorage.removeItem("token");
localStorage.removeItem("username");

console.error("app: Failed to refresh token.");
});
}, 10000);

return () => clearInterval(interval);
}, []);

return (
<>
<Header />
Expand Down
43 changes: 2 additions & 41 deletions src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect } from "react";
import React, { useEffect, useState } from "react";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import { ReactKeycloakProvider } from "@react-keycloak/web";
import ReactDOM from "react-dom/client";
Expand Down Expand Up @@ -27,16 +27,13 @@ const Main = () => {
const base_url = import.meta.env.PROD
? "/search/serviceWorker.js"
: "/serviceWorker.js";
const scope_url = import.meta.env.PROD ? "/search/" : "/search/";

const registerServiceWorker = async () => {
if ("serviceWorker" in navigator) {
try {
const registration = await navigator.serviceWorker.register(base_url, {
scope: scope_url,
scope: "/search/",
});
console.log("scope", registration.scope);
console.log("controlled", navigator.serviceWorker.controller);

await registration.active.postMessage({
type: "TOKEN",
Expand All @@ -49,42 +46,6 @@ const Main = () => {
};

registerServiceWorker();

// if ("serviceWorker" in navigator) {
// window.addEventListener("load", () => {
// navigator.serviceWorker
// .register(base_url)
// .then((registration) => {
// console.log(
// "Service Worker registered with scope: ",
// registration.scope
// );
// if (token && registration.active) {
// console.log("registration.active");

// registration.active.postMessage({
// type: "SET_TOKEN",
// token: token,
// });
// }
// })
// .catch((error) => {
// console.log("ServiceWorker registration failed: ", error);
// });
// });
// }

// if (navigator.serviceWorker.controller) {
// console.log("post message", navigator);

// navigator.serviceWorker.controller.postMessage({
// type: "SET_TOKEN",
// token: token,
// });
// } else {
// console.log("no message", navigator.serviceWorker);
// }

return <></>;
};

Expand Down

0 comments on commit 0f751e0

Please sign in to comment.