- Updated multiple components to utilize useAuthQuery instead of useQuery for fetching user-related data, enhancing authentication management. - Adjusted balance and subscription queries across various components including InitUser, ManageSubscription, PricingCards, CreditDisplay, and others to ensure consistent authentication checks. - Improved overall code maintainability by centralizing authentication logic in the new hook.
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
"use client";
|
|
|
|
import { authClient } from "@/lib/auth-client";
|
|
import { useMutation } from "convex/react";
|
|
import { useAuthQuery } from "@/hooks/use-auth-query";
|
|
import { api } from "@/convex/_generated/api";
|
|
import { useEffect, useRef } from "react";
|
|
import { toast } from "@/lib/toast";
|
|
import { msg } from "@/lib/toast-messages";
|
|
|
|
/**
|
|
* Initialisiert die Credit-Balance für neue User.
|
|
* Wird einmal im Layout eingebunden und sorgt dafür,
|
|
* dass jeder eingeloggte User eine Balance + Free-Subscription hat.
|
|
*/
|
|
export function InitUser() {
|
|
const { data: session } = authClient.useSession();
|
|
|
|
const balance = useAuthQuery(api.credits.getBalance);
|
|
const initBalance = useMutation(api.credits.initBalance);
|
|
const initStartedRef = useRef(false);
|
|
|
|
useEffect(() => {
|
|
if (
|
|
!session?.user ||
|
|
!balance ||
|
|
balance.balance !== 0 ||
|
|
balance.monthlyAllocation !== 0
|
|
) {
|
|
return;
|
|
}
|
|
if (initStartedRef.current) return;
|
|
initStartedRef.current = true;
|
|
|
|
void initBalance()
|
|
.then(() => {
|
|
toast.success(msg.auth.initialSetup.title, msg.auth.initialSetup.desc);
|
|
})
|
|
.catch(() => {
|
|
initStartedRef.current = false;
|
|
});
|
|
}, [session?.user, balance, initBalance]);
|
|
|
|
return null;
|
|
}
|