aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/components/utils/ValidAccountCheck.tsx
blob: 54d27b342c4ea7d5ed8ff4a55038d6fe9c634042 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"use client";

import { useEffect } from "react";
import { useRouter } from "next/navigation";
import { useQuery } from "@tanstack/react-query";

import { useTRPC } from "@karakeep/shared-react/trpc";

/**
 * 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 api = useTRPC();
  const router = useRouter();
  const { error } = useQuery(
    api.users.whoami.queryOptions(undefined, {
      retry: (_failureCount, error) => {
        if (error.data?.code === "UNAUTHORIZED") {
          return false;
        }
        return true;
      },
    }),
  );
  useEffect(() => {
    if (error?.data?.code === "UNAUTHORIZED") {
      router.push("/logout");
    }
  }, [error]);

  return <></>;
}