From 1f343abbb552e12edcbf2d884eb9e8386172a4d6 Mon Sep 17 00:00:00 2001 From: Darin Kelkhoff Date: Mon, 25 Sep 2023 16:27:46 -0500 Subject: [PATCH] Switch to use jwt_decode library (from auth0) rather than S/O decodeJWT function --- package.json | 1 + src/App.tsx | 17 +++-------------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index dc95524..f963e83 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "html-react-parser": "1.4.8", "html-to-text": "^9.0.5", "http-proxy-middleware": "2.0.6", + "jwt-decode": "3.1.2", "rapidoc": "9.3.4", "react": "18.0.0", "react-ace": "10.1.0", diff --git a/src/App.tsx b/src/App.tsx index 8b2d4cb..edf1372 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -33,6 +33,7 @@ import CssBaseline from "@mui/material/CssBaseline"; import Icon from "@mui/material/Icon"; import {ThemeProvider} from "@mui/material/styles"; import {LicenseInfo} from "@mui/x-license-pro"; +import jwt_decode from "jwt-decode"; import React, {JSXElementConstructor, Key, ReactElement, useEffect, useState,} from "react"; import {useCookies} from "react-cookie"; import {Navigate, Route, Routes, useLocation,} from "react-router-dom"; @@ -72,18 +73,6 @@ export default function App() const [loggedInUser, setLoggedInUser] = useState({} as { name?: string, email?: string }); const [defaultRoute, setDefaultRoute] = useState("/no-apps"); - const decodeJWT = (jwt: string): any => - { - const base64Url = jwt.split(".")[1]; - const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/"); - const jsonPayload = decodeURIComponent(window.atob(base64).split("").map(function (c) - { - return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2); - }).join("")); - - return JSON.parse(jsonPayload); - }; - const shouldStoreNewToken = (newToken: string, oldToken: string): boolean => { if (!oldToken) @@ -93,8 +82,8 @@ export default function App() try { - const oldJSON = decodeJWT(oldToken); - const newJSON = decodeJWT(newToken); + const oldJSON: any = jwt_decode(oldToken); + const newJSON: any = jwt_decode(newToken); //////////////////////////////////////////////////////////////////////////////////// // if the old (local storage) token is expired, then we need to store the new one //