aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/components/utils/ValidAccountCheck.tsx
blob: 12c11087c0a9959450f72c2941523e835d82129d (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
"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 <></>;
}