From 0989065d97ec0df43fa14e6cd83476aec387facb Mon Sep 17 00:00:00 2001 From: Manuel Doneda Date: Fri, 23 Feb 2024 14:13:49 +0100 Subject: [PATCH] fix: :bug: Handle invalid json from local storage --- src/context/UserContext.tsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/context/UserContext.tsx b/src/context/UserContext.tsx index 3d5c05b..31fff2a 100644 --- a/src/context/UserContext.tsx +++ b/src/context/UserContext.tsx @@ -34,8 +34,12 @@ function getUserDataFromStorage(): LoginResponse | null { const rawData = localStorage.getItem(STORAGE_KEY) || sessionStorage.getItem(STORAGE_KEY); if (!rawData) return null; // Not logged in - const parsed = JSON.parse(rawData); - return parsed; + try { + return JSON.parse(rawData); + } + catch { + return null; + } } export function UserContextProvider({ children }: { children: JSX.Element; }) {