Removes Tanstack Query

This commit is contained in:
2025-07-15 10:53:28 -07:00
parent 8f96062058
commit 762306023b
8 changed files with 85 additions and 249 deletions

View File

@@ -1,5 +1,4 @@
import { createContext, useContext, useCallback } from "react";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { createContext, useContext, useCallback, useState } from "react";
import type { ReactNode } from "react";
import { pb } from "@/lib/pocketbase";
import type { AuthRecord } from "pocketbase";
@@ -26,91 +25,47 @@ export interface AuthContextValue {
const AuthContext = createContext<AuthContextValue | undefined>(undefined);
/**
* Fetches the currently authenticated user from PocketBase.
*/
async function fetchUser(): Promise<AuthRecord | null> {
if (pb.authStore.isValid) {
return pb.authStore.record;
}
return null;
}
/**
* Provider for authentication context, using TanStack Query for state management.
* Provider for authentication context.
*/
export function AuthProvider({ children }: { children: ReactNode }) {
const queryClient = useQueryClient();
const { data: user, isLoading } = useQuery({
queryKey: ["auth", "user"],
queryFn: fetchUser,
});
const [isLoading, setIsLoading] = useState(false);
const [user, setUser] = useState<AuthRecord | null>(pb.authStore.record);
const navigate = useNavigate();
const loginMutation = useMutation({
mutationFn: async ({
email,
password,
}: {
email: string;
password: string;
}) => {
await pb.collection("users").authWithPassword(email, password);
return fetchUser();
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["auth", "user"] });
},
});
function updateUser() {
if (pb.authStore.isValid) {
setUser(pb.authStore.record);
}
setIsLoading(false);
}
const signupMutation = useMutation({
mutationFn: async ({
email,
password,
passwordConfirm,
}: {
email: string;
password: string;
passwordConfirm: string;
}) => {
await pb.collection("users").create({ email, password, passwordConfirm });
await pb.collection("users").authWithPassword(email, password);
return fetchUser();
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["auth", "user"] });
},
});
const logoutMutation = useMutation({
mutationFn: async () => {
pb.authStore.clear();
return null;
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["auth", "user"] });
},
});
const login = useCallback(
async (email: string, password: string) => {
await loginMutation.mutateAsync({ email, password });
navigate({ to: "/campaigns" });
},
[loginMutation],
);
const login = useCallback(async (email: string, password: string) => {
console.log("login");
setIsLoading(true);
await pb.collection("users").authWithPassword(email, password);
updateUser();
navigate({ to: "/campaigns" });
}, []);
const signup = useCallback(
async (email: string, password: string, passwordConfirm: string) => {
await signupMutation.mutateAsync({ email, password, passwordConfirm });
console.log("signup");
setIsLoading(true);
await pb.collection("users").create({ email, password, passwordConfirm });
await pb.collection("users").authWithPassword(email, password);
updateUser();
navigate({ to: "/campaigns" });
},
[signupMutation],
[],
);
const logout = useCallback(async () => {
await logoutMutation.mutateAsync();
console.log("logout");
pb.authStore.clear();
setUser(null);
navigate({ to: "/" });
}, [logoutMutation]);
}, []);
return (
<AuthContext.Provider