Skip to content

Commit

Permalink
Update token when request is made
Browse files Browse the repository at this point in the history
  • Loading branch information
sergesoroka committed Nov 13, 2024
1 parent 9e5cbbe commit 3a7b6e8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 20 deletions.
13 changes: 13 additions & 0 deletions components/UploadFile/UploadFile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@ import { useEffect, useState } from "react";
import Close from "../Icons/Close";
import Spinner from "../Icons/Spinner";

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

// eslint-disable-next-line react/prop-types
export default function UploadFile({ setImageData, setType, file, setFile }) {
const fileQuery = `${import.meta.env.VITE_BaseURL}db/download?what=knnquery`;

const [isNotRightFile, setIsNotRightFile] = useState(false);
const [isLoading, setIsLoading] = useState(false);

const { keycloak } = useKeycloak();

useEffect(() => {
async function fetchDate() {
try {
await keycloak.updateToken(30);
console.log("Upload file Comp: UPDATED");
localStorage.setItem("token", keycloak.token);
} catch (error) {
console.error("Upload file Comp: Failed to refresh token:", error);
}

const formData = new FormData();
formData.append("files", file);

const response = await fetch(fileQuery, {
method: "POST",
body: formData,
Expand Down
1 change: 1 addition & 0 deletions public/serviceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ self.addEventListener("activate", (event) => {
self.addEventListener("message", (event) => {
if (event.data && event.data.type === "TOKEN") {
accessToken = event.data.token;
console.log(accessToken);
}
});

Expand Down
36 changes: 18 additions & 18 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ 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(() => {
console.error("app: Failed to refresh token.");
});
}, 10000);
// 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(() => {
// console.error("app: Failed to refresh token.");
// });
// }, 10000);

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

return (
<>
Expand Down
5 changes: 3 additions & 2 deletions src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ const Main = () => {
}
}
};

registerServiceWorker();
useEffect(() => {
registerServiceWorker();
}, [token]);
return <></>;
};

Expand Down
20 changes: 20 additions & 0 deletions utils/useFetch.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState, useEffect } from "react";
import { useKeycloak } from "@react-keycloak/web";
import axios from "axios";
import { useNavigate } from "react-router-dom";

function useFetch(url) {
const [data, setData] = useState(null);
Expand All @@ -9,6 +10,7 @@ function useFetch(url) {
const [auth, setAuth] = useState(false);

const { keycloak } = useKeycloak();
const navigate = useNavigate();

const stored_token = localStorage.getItem("token");
const kc_token = keycloak.token ? keycloak.token : stored_token;
Expand All @@ -23,6 +25,7 @@ function useFetch(url) {
function (config) {
if (kc_token) {
config.headers.Authorization = `Bearer ${kc_token}`;
console.log("axios interseption");
}
return config;
},
Expand All @@ -38,12 +41,29 @@ function useFetch(url) {
}

if (kc_token) {
keycloak
.updateToken(30)
.then((refreshed) => {
if (refreshed) {
console.log(
"useFetch: Token refreshed and updated in localStorage."
);
localStorage.setItem("token", keycloak.token);
} else {
console.log("useFetch: Token is still valid.");
}
})
.catch(() => {
console.error("useFetch: Failed to refresh token.");
});
axiosInstance
.get(url)
.then((response) => {
setData(response.data);
})
.catch((error) => {
// navigate("/");
// keycloak.login();
console.error("Error fetching data:", error);
})
.finally(() => {
Expand Down

0 comments on commit 3a7b6e8

Please sign in to comment.