|
1 | | -import { Routes, Route } from "react-router-dom"; |
| 1 | +import { Routes, Route, useNavigate } from "react-router-dom"; |
2 | 2 | import Login from "./routes/Login/Login.tsx"; |
3 | 3 | import { AuthenticationGuard } from "./providers/AuthGuard.tsx"; |
4 | | -import { lazy, Suspense } from "react"; |
| 4 | +import { lazy, Suspense, useEffect, useState } from "react"; |
| 5 | +import { useAuth0 } from "@auth0/auth0-react"; |
| 6 | +import { ACCESS_TOKEN, REFRESH_TOKEN } from "./lib/constants.ts"; |
| 7 | +import { useMutation } from "@tanstack/react-query"; |
| 8 | +import axiosInstance from "./config/axios-config.ts"; |
5 | 9 | const Text = lazy(() => import("./routes/Text/Text.tsx")); |
6 | 10 | const Instance = lazy(() => import("./routes/Instance/Instance.tsx")); |
7 | 11 |
|
| 12 | +type Auth0UserType = { |
| 13 | + getIdTokenClaims: () => Promise<any>; |
| 14 | + isAuthenticated: boolean; |
| 15 | + logout: (options?: { logoutParams?: { returnTo: string } }) => Promise<void>; |
| 16 | +}; |
| 17 | + |
8 | 18 | function App() { |
| 19 | + const navigate = useNavigate(); |
| 20 | + const tokenRefreshIntervalMs = Number(60000) || 0; |
| 21 | + |
| 22 | + const [intervalId, setIntervalId] = useState<ReturnType< |
| 23 | + typeof setInterval |
| 24 | + > | null>(null); |
| 25 | + const { getIdTokenClaims, isAuthenticated, logout }: Auth0UserType = |
| 26 | + useAuth0() as Auth0UserType; |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + if (isAuthenticated) { |
| 30 | + const getToken = async () => { |
| 31 | + try { |
| 32 | + const claims = await getIdTokenClaims(); |
| 33 | + const idToken = claims.__raw; |
| 34 | + if (Date.now() >= claims.exp * 1000) { |
| 35 | + sessionStorage.removeItem(ACCESS_TOKEN); |
| 36 | + localStorage.removeItem(REFRESH_TOKEN); |
| 37 | + isAuthenticated && |
| 38 | + (await logout({ |
| 39 | + logoutParams: { |
| 40 | + returnTo: globalThis.location.origin, |
| 41 | + }, |
| 42 | + })); |
| 43 | + } else { |
| 44 | + sessionStorage.setItem(ACCESS_TOKEN, idToken); |
| 45 | + } |
| 46 | + } catch (error) { |
| 47 | + console.error("Error fetching token:", error); |
| 48 | + } |
| 49 | + }; |
| 50 | + getToken().then(); |
| 51 | + } |
| 52 | + }, [isAuthenticated]); |
| 53 | + |
| 54 | + const loginMutation = useMutation({ |
| 55 | + mutationFn: async (refreshToken: string) => { |
| 56 | + const response = await axiosInstance.post("/api/v1/auth/refresh-token", { |
| 57 | + token: refreshToken, |
| 58 | + }); |
| 59 | + return response.data; |
| 60 | + }, |
| 61 | + onSuccess: (data: { access_token: string }) => { |
| 62 | + sessionStorage.setItem(ACCESS_TOKEN, data.access_token); |
| 63 | + if (!intervalId) { |
| 64 | + startTokenRefreshCounter(); |
| 65 | + } |
| 66 | + }, |
| 67 | + onError: () => { |
| 68 | + sessionStorage.removeItem(ACCESS_TOKEN); |
| 69 | + localStorage.removeItem(REFRESH_TOKEN); |
| 70 | + navigate("/login"); |
| 71 | + }, |
| 72 | + }); |
| 73 | + |
| 74 | + const startTokenRefreshCounter = () => { |
| 75 | + const interval = setInterval(() => { |
| 76 | + const refreshToken = localStorage.getItem(REFRESH_TOKEN); |
| 77 | + if (refreshToken) { |
| 78 | + loginMutation.mutate(refreshToken); |
| 79 | + } |
| 80 | + }, tokenRefreshIntervalMs); |
| 81 | + setIntervalId(interval); |
| 82 | + }; |
9 | 83 | return ( |
10 | 84 | <div className="flex w-full"> |
11 | 85 | <div className="flex-1 overflow-auto"> |
|
0 commit comments