All files / app/features/userQueries useCurrentUser.ts

80% Statements 12/15
33.33% Branches 2/6
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 2112x 12x 12x 12x 12x   303x 296x 296x 296x   296x 72x           296x    
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.");
      if (typeof window !== "undefined") router.push(loginRoute);
    }
  }, [authState.userId, router]);
 
  return userQuery;
}