diff --git a/src/main/web/src/organisms/header/Header.tsx b/src/main/web/src/organisms/header/Header.tsx index b71094c7..24d33ad0 100644 --- a/src/main/web/src/organisms/header/Header.tsx +++ b/src/main/web/src/organisms/header/Header.tsx @@ -4,7 +4,7 @@ import {Link, useLocation} from 'react-router-dom'; import ModalLoginContainer from '../login/ModalLoginContainer'; import {Notifications} from "./Notifications"; import SignUp from "../signup/SignUp"; -import {isUserLoggedIn} from "../../services/AuthService"; +import {isUserLoggedIn, isTokenValid, logout} from "../../services/AuthService"; import "./Header.css"; import {Tooltip} from "react-tooltip"; import config from "../../config/config"; @@ -41,6 +41,13 @@ export const Header = () => { fetchAndSetNotifications(); }, []); + useEffect((): void => { + if (isUserLoggedIn() && !isTokenValid()) { + logout(); + window.location.href = "/"; + } + }, []); + useEffect((): void => { setCurrentLocation(location.pathname); }, [location]); diff --git a/src/main/web/src/services/AuthService.tsx b/src/main/web/src/services/AuthService.tsx index 6b8d273c..2aa41828 100644 --- a/src/main/web/src/services/AuthService.tsx +++ b/src/main/web/src/services/AuthService.tsx @@ -1,5 +1,7 @@ import axios from "axios"; import config from "../config/config"; +import {CredentialResponse} from "@react-oauth/google"; +import {jwtDecode} from "jwt-decode"; export const register = (username: string, email: string, password: string) => { return axios.post(config.apiUrl + "api/auth/signup", { @@ -106,4 +108,19 @@ export const isUserLoggedIn = (): boolean => { } else { return false; } -}; \ No newline at end of file +}; + +export const isTokenValid = (): boolean => { + const jwtToken: string | null = localStorage.getItem("token"); + + if (jwtToken) { + const decodedToken = jwtDecode(jwtToken); + if (decodedToken && decodedToken.exp) { + return decodedToken.exp * 1000 > Date.now(); + } else { + return false; + } + } else { + return false; + } +};