import { trpc } from "../../lib/trpc";

export function useAuth() {
  const utils = trpc.useUtils();
  const { data: usuario, isLoading } = trpc.auth.me.useQuery();

  const login = trpc.auth.login.useMutation({
    onSuccess: () => utils.auth.me.invalidate(),
  });

  const logout = trpc.auth.logout.useMutation({
    onSuccess: () => utils.auth.me.invalidate(),
  });

  const podePublicar = usuario?.papel === "editor" || usuario?.papel === "admin";

  return { usuario, isLoading, login, logout, podePublicar };
}
