Skip to content

Commit

Permalink
Update thoken when window is on focus
Browse files Browse the repository at this point in the history
  • Loading branch information
sergesoroka committed Nov 13, 2024
1 parent f120f1f commit 47d84c9
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,40 @@ 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 handleFocus = () => {
console.log("on focus");
keycloak
.updateToken(30)
.then((refreshed) => {
if (refreshed) {
console.log(
"App on focus: Token refreshed and updated in localStorage.",
keycloak.token
);
localStorage.setItem("token", keycloak.token);
} else {
console.log("app: Token is still valid.");
}
})
.catch(() => {
console.error("App on focus: Failed to refresh token.");
// keycloak.logout();
});
};

const handleBlur = () => {
console.log("blur");
};

window.addEventListener("focus", handleFocus);
window.addEventListener("blur", handleBlur);

// return () => clearInterval(interval);
// }, []);
return () => {
window.removeEventListener("focus", handleFocus);
window.removeEventListener("blur", handleBlur);
};
});

return (
<>
Expand Down

0 comments on commit 47d84c9

Please sign in to comment.