From aa8201eaef629fb14890773616146bda82820d1a Mon Sep 17 00:00:00 2001 From: isra el Date: Mon, 15 Apr 2024 01:26:26 +0300 Subject: [PATCH] chore(web): check for token expiration on launch and redirect to login --- web/components/Navbar.tsx | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/web/components/Navbar.tsx b/web/components/Navbar.tsx index e64dfd8..ef4be3b 100644 --- a/web/components/Navbar.tsx +++ b/web/components/Navbar.tsx @@ -19,12 +19,30 @@ import Router from 'next/router' import { useDispatch, useSelector } from 'react-redux' import { logout, selectAuthUser } from '../store/authSlice' import Image from 'next/image' +import { useEffect } from 'react' +import { authService } from '../services/authService' export default function Navbar() { const dispatch = useDispatch() const { colorMode, toggleColorMode } = useColorMode() const authUser = useSelector(selectAuthUser) + useEffect(() => { + const timout = setTimeout(async () => { + if (authUser) { + authService + .whoAmI() + .catch((e) => { + if (e.response.status === 401) { + dispatch(logout()) + } + }) + .then((res) => {}) + } + }, 5000) + return () => clearTimeout(timout) + }, [authUser, dispatch]) + return ( <>