From 827a29d074cd66f1533f24b3980d537dc3947ad1 Mon Sep 17 00:00:00 2001
From: timothycarambat <rambat1010@gmail.com>
Date: Thu, 5 Oct 2023 15:16:14 -0700
Subject: [PATCH] improve AUTH_TIMESTAMP clearing

---
 frontend/src/AuthContext.jsx                               | 3 ++-
 frontend/src/components/Modals/Password/index.jsx          | 7 ++++++-
 .../src/components/Modals/Settings/MultiUserMode/index.jsx | 7 ++++++-
 .../Modals/Settings/PasswordProtection/index.jsx           | 7 ++++++-
 frontend/src/components/PrivateRoute/index.jsx             | 3 ++-
 frontend/src/components/Sidebar/index.jsx                  | 3 ++-
 6 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/frontend/src/AuthContext.jsx b/frontend/src/AuthContext.jsx
index 219fbea40..23e25a358 100644
--- a/frontend/src/AuthContext.jsx
+++ b/frontend/src/AuthContext.jsx
@@ -1,5 +1,5 @@
 import React, { useState, createContext } from "react";
-import { AUTH_TOKEN, AUTH_USER } from "./utils/constants";
+import { AUTH_TIMESTAMP, AUTH_TOKEN, AUTH_USER } from "./utils/constants";
 
 export const AuthContext = createContext(null);
 export function ContextWrapper(props) {
@@ -19,6 +19,7 @@ export function ContextWrapper(props) {
     unsetUser: () => {
       localStorage.removeItem(AUTH_USER);
       localStorage.removeItem(AUTH_TOKEN);
+      localStorage.removeItem(AUTH_TIMESTAMP);
       setStore({ user: null, authToken: null });
     },
   });
diff --git a/frontend/src/components/Modals/Password/index.jsx b/frontend/src/components/Modals/Password/index.jsx
index 11da0fc27..096906e8d 100644
--- a/frontend/src/components/Modals/Password/index.jsx
+++ b/frontend/src/components/Modals/Password/index.jsx
@@ -2,7 +2,11 @@ import React, { useState, useEffect } from "react";
 import System from "../../../models/system";
 import SingleUserAuth from "./SingleUserAuth";
 import MultiUserAuth from "./MultiUserAuth";
-import { AUTH_TOKEN, AUTH_USER } from "../../../utils/constants";
+import {
+  AUTH_TIMESTAMP,
+  AUTH_TOKEN,
+  AUTH_USER,
+} from "../../../utils/constants";
 
 export default function PasswordModal({ mode = "single" }) {
   return (
@@ -50,6 +54,7 @@ export function usePasswordModal() {
             });
             window.localStorage.removeItem(AUTH_USER);
             window.localStorage.removeItem(AUTH_TOKEN);
+            window.localStorage.removeItem(AUTH_TIMESTAMP);
             return;
           } else {
             setAuth({
diff --git a/frontend/src/components/Modals/Settings/MultiUserMode/index.jsx b/frontend/src/components/Modals/Settings/MultiUserMode/index.jsx
index 4f93d9759..c54879fef 100644
--- a/frontend/src/components/Modals/Settings/MultiUserMode/index.jsx
+++ b/frontend/src/components/Modals/Settings/MultiUserMode/index.jsx
@@ -1,6 +1,10 @@
 import React, { useState } from "react";
 import System from "../../../../models/system";
-import { AUTH_TOKEN, AUTH_USER } from "../../../../utils/constants";
+import {
+  AUTH_TIMESTAMP,
+  AUTH_TOKEN,
+  AUTH_USER,
+} from "../../../../utils/constants";
 import paths from "../../../../utils/paths";
 
 const noop = () => false;
@@ -29,6 +33,7 @@ export default function MultiUserMode({ hideModal = noop }) {
       setTimeout(() => {
         window.localStorage.removeItem(AUTH_USER);
         window.localStorage.removeItem(AUTH_TOKEN);
+        window.localStorage.removeItem(AUTH_TIMESTAMP);
         window.location = paths.admin.users();
       }, 2_000);
       return;
diff --git a/frontend/src/components/Modals/Settings/PasswordProtection/index.jsx b/frontend/src/components/Modals/Settings/PasswordProtection/index.jsx
index 3b1a92dce..a9de2cd6f 100644
--- a/frontend/src/components/Modals/Settings/PasswordProtection/index.jsx
+++ b/frontend/src/components/Modals/Settings/PasswordProtection/index.jsx
@@ -1,6 +1,10 @@
 import React, { useState } from "react";
 import System from "../../../../models/system";
-import { AUTH_TOKEN, AUTH_USER } from "../../../../utils/constants";
+import {
+  AUTH_TIMESTAMP,
+  AUTH_TOKEN,
+  AUTH_USER,
+} from "../../../../utils/constants";
 import showToast from "../../../../utils/toast";
 
 const noop = () => false;
@@ -28,6 +32,7 @@ export default function PasswordProtection({
       setTimeout(() => {
         window.localStorage.removeItem(AUTH_USER);
         window.localStorage.removeItem(AUTH_TOKEN);
+        window.localStorage.removeItem(AUTH_TIMESTAMP);
         window.location.reload();
       }, 3_000);
       return;
diff --git a/frontend/src/components/PrivateRoute/index.jsx b/frontend/src/components/PrivateRoute/index.jsx
index 33f5c6337..0829dfd5e 100644
--- a/frontend/src/components/PrivateRoute/index.jsx
+++ b/frontend/src/components/PrivateRoute/index.jsx
@@ -3,7 +3,7 @@ import { Navigate } from "react-router-dom";
 import { FullScreenLoader } from "../Preloader";
 import validateSessionTokenForUser from "../../utils/session";
 import paths from "../../utils/paths";
-import { AUTH_TOKEN, AUTH_USER } from "../../utils/constants";
+import { AUTH_TIMESTAMP, AUTH_TOKEN, AUTH_USER } from "../../utils/constants";
 import { userFromStorage } from "../../utils/request";
 import System from "../../models/system";
 
@@ -31,6 +31,7 @@ function useIsAuthenticated() {
       if (!isValid) {
         localStorage.removeItem(AUTH_USER);
         localStorage.removeItem(AUTH_TOKEN);
+        localStorage.removeItem(AUTH_TIMESTAMP);
         setIsAuthed(false);
         return;
       }
diff --git a/frontend/src/components/Sidebar/index.jsx b/frontend/src/components/Sidebar/index.jsx
index 311208127..438df1ad3 100644
--- a/frontend/src/components/Sidebar/index.jsx
+++ b/frontend/src/components/Sidebar/index.jsx
@@ -21,7 +21,7 @@ import paths from "../../utils/paths";
 import Discord from "../Icons/Discord";
 import useUser from "../../hooks/useUser";
 import { userFromStorage } from "../../utils/request";
-import { AUTH_TOKEN, AUTH_USER } from "../../utils/constants";
+import { AUTH_TIMESTAMP, AUTH_TOKEN, AUTH_USER } from "../../utils/constants";
 import useLogo from "../../hooks/useLogo";
 import SettingsOverlay, { useSystemSettingsOverlay } from "./SettingsOverlay";
 
@@ -320,6 +320,7 @@ function LogoutButton() {
       onClick={() => {
         window.localStorage.removeItem(AUTH_USER);
         window.localStorage.removeItem(AUTH_TOKEN);
+        window.localStorage.removeItem(AUTH_TIMESTAMP);
         window.location.replace(paths.home());
       }}
       className="flex flex-grow w-[100%] h-[36px] gap-x-2 py-[5px] px-4 border border-slate-400 dark:border-transparent rounded-lg text-slate-800 dark:text-slate-200 justify-center items-center hover:bg-slate-100 dark:bg-stone-800 dark:hover:bg-stone-900"
-- 
GitLab