All files / app/features/userQueries useCurrentUser.ts

80% Statements 12/15
25% Branches 1/4
100% Functions 2/2
85.71% Lines 12/14

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 2111x 11x 11x 11x 11x   455x 448x 448x 448x   448x 71x           448x    
import { useAuthContext } from "features/auth/AuthProvider";
import { useUser } from "features/userQueries/useUsers";
import { useRouter } from "next/router";
import { useEffect } from "react";
import { loginRoute } from "routes";
 
export default function useCurrentUser() {
  const authState = useAuthContext().authState;
  const userQuery = useUser(authState.userId ?? undefined);
  const router = useRouter();
 
  useEffect(() => {
    Iif (!authState.userId) {
      console.error("No user id available to get current user.");
      Iif (typeof window !== "undefined") router.push(loginRoute);
    }
  }, [authState.userId, router]);
 
  return userQuery;
}