From f1c956a361539592d00836488181b69218798600 Mon Sep 17 00:00:00 2001
From: MohamedBassem <me@mbassem.com>
Date: Sat, 5 Oct 2024 17:14:59 +0000
Subject: feature(web): Async validate JWT account and sign out the user if
 they no longer exist

---
 apps/web/components/utils/ValidAccountCheck.tsx | 26 +++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
 create mode 100644 apps/web/components/utils/ValidAccountCheck.tsx

(limited to 'apps/web/components/utils/ValidAccountCheck.tsx')

diff --git a/apps/web/components/utils/ValidAccountCheck.tsx b/apps/web/components/utils/ValidAccountCheck.tsx
new file mode 100644
index 00000000..12c11087
--- /dev/null
+++ b/apps/web/components/utils/ValidAccountCheck.tsx
@@ -0,0 +1,26 @@
+"use client";
+
+import { api } from "@/lib/trpc";
+import { signOut } from "next-auth/react";
+
+/**
+ * This component is used to address a confusion when the JWT token exists but the user no longer exists in the database.
+ * So this component synchronusly checks if the user is still valid and if not, signs out the user.
+ */
+export default function ValidAccountCheck() {
+  const { error } = api.users.whoami.useQuery(undefined, {
+    retry: (_failureCount, error) => {
+      if (error.data?.code === "UNAUTHORIZED") {
+        return false;
+      }
+      return true;
+    },
+  });
+  if (error?.data?.code === "UNAUTHORIZED") {
+    signOut({
+      callbackUrl: "/",
+    });
+  }
+
+  return <></>;
+}
-- 
cgit v1.2.3-70-g09d2

