Просмотр исходного кода

improvement(frontend): Update app behavior with invalid tokens (#4286)

sp.wack 1 год назад
Родитель
Сommit
a6993b7bf5

+ 4 - 4
frontend/src/root.tsx

@@ -128,10 +128,10 @@ export default function App() {
   }, [user]);
 
   React.useEffect(() => {
-    // If the user is on the home page, we should stop the socket connection.
-    // This is relevant when the user redirects here for whatever reason.
-    if (location.pathname === "/" && isConnected) {
-      stop();
+    if (location.pathname === "/") {
+      // If the user is on the home page, we should stop the socket connection.
+      // This is relevant when the user redirects here for whatever reason.
+      if (isConnected) stop();
     }
   }, [location.pathname]);
 

+ 3 - 0
frontend/src/routes/_index/route.tsx

@@ -72,6 +72,9 @@ function GitHubAuth({
 }
 
 export const clientLoader = async ({ request }: ClientLoaderFunctionArgs) => {
+  const token = localStorage.getItem("token");
+  if (token) return redirect("/app");
+
   const ghToken = localStorage.getItem("ghToken");
   let repositories: GitHubRepository[] = [];
   if (ghToken) {

+ 2 - 0
frontend/src/routes/app.tsx

@@ -10,6 +10,7 @@ import {
 } from "@remix-run/react";
 import { useDispatch, useSelector } from "react-redux";
 import WebSocket from "ws";
+import toast from "react-hot-toast";
 import ChatInterface from "#/components/chat/ChatInterface";
 import { getSettings } from "#/services/settings";
 import Security from "../components/modals/security/Security";
@@ -181,6 +182,7 @@ function App() {
       }
 
       if ("error" in parsed) {
+        toast.error(parsed.error);
         fetcher.submit({}, { method: "POST", action: "/end-session" });
         return;
       }

+ 2 - 6
frontend/src/routes/end-session.ts

@@ -1,11 +1,7 @@
 import { redirect } from "@remix-run/react";
+import { clearSession } from "#/utils/clear-session";
 
 export const clientAction = () => {
-  const token = localStorage.getItem("token");
-  const repo = localStorage.getItem("repo");
-
-  if (token) localStorage.removeItem("token");
-  if (repo) localStorage.removeItem("repo");
-
+  clearSession();
   return redirect("/");
 };

+ 7 - 0
frontend/src/utils/clear-session.ts

@@ -0,0 +1,7 @@
+/**
+ * Clear the session data from the local storage. This will remove the token and repo
+ */
+export const clearSession = () => {
+  localStorage.removeItem("token");
+  localStorage.removeItem("repo");
+};