All files / react-app/src/features/welcome utils.ts

6.25% Statements 1/16
0% Branches 0/12
0% Functions 0/3
6.25% Lines 1/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 34 35 36          1x                                                            
import { QueryClient } from "@tanstack/react-query";
import { redirect } from "react-router";
 
import { getUser } from "@/api";
 
export const loader = (queryClient: QueryClient, loginFlag?: boolean) => {
  return async () => {
    const queryString = window.location.search;
    // Parse the query string to get URL parameters
    const queryParams = new URLSearchParams(queryString);
    // Access specific parameters
    const errorDescription = queryParams.get("error_description");
    const error = queryParams.get("error");
    if (errorDescription || error) {
      console.error("Authentication Error:", { errorDescription, error });
      return { error };
    }
 
    // check user query has been initialized
    if (!queryClient.getQueryData(["user"])) {
      const userFetch = await queryClient.fetchQuery({
        queryKey: ["user"],
        queryFn: () => getUser(),
      });
      const isUserLoggedIn =
        !["/login", "/faq", "/support"].includes(window.location.pathname) && !userFetch?.user;
 
      if (!loginFlag && isUserLoggedIn) {
        return redirect("/login");
      }
    }
 
    return queryClient.getQueryData(["user"]);
  };
};