|
| 1 | +import { |
| 2 | + useMutation, |
| 3 | + useQuery, |
| 4 | + useQueryClient, |
| 5 | + useSuspenseQuery, |
| 6 | +} from "@tanstack/react-query" |
| 7 | +import { createCacheConfig, type CacheConfig } from "../shared/cache-config" |
| 8 | +import type { QueryNamespace } from "../shared/query-keys" |
| 9 | +import { createAuthQueryKeys } from "./query-keys" |
| 10 | +import type { |
| 11 | + AuthQueryInput, |
| 12 | + AuthQueryKeys, |
| 13 | + AuthService, |
| 14 | + UseAuthResult, |
| 15 | + UseSuspenseAuthResult, |
| 16 | +} from "./types" |
| 17 | + |
| 18 | +export type CreateAuthHooksConfig< |
| 19 | + TCustomer, |
| 20 | + TLoginInput, |
| 21 | + TRegisterInput, |
| 22 | + TUpdateInput, |
| 23 | +> = { |
| 24 | + service: AuthService<TCustomer, TLoginInput, TRegisterInput, TUpdateInput> |
| 25 | + queryKeys?: AuthQueryKeys |
| 26 | + queryKeyNamespace?: QueryNamespace |
| 27 | + cacheConfig?: CacheConfig |
| 28 | +} |
| 29 | + |
| 30 | +export type AuthMutationOptions<TData, TVariables> = { |
| 31 | + onSuccess?: (data: TData, variables: TVariables) => void |
| 32 | + onError?: (error: unknown) => void |
| 33 | +} |
| 34 | + |
| 35 | +export function createAuthHooks< |
| 36 | + TCustomer, |
| 37 | + TLoginInput, |
| 38 | + TRegisterInput, |
| 39 | + TUpdateInput, |
| 40 | +>({ |
| 41 | + service, |
| 42 | + queryKeys, |
| 43 | + queryKeyNamespace = "storefront-data", |
| 44 | + cacheConfig, |
| 45 | +}: CreateAuthHooksConfig<TCustomer, TLoginInput, TRegisterInput, TUpdateInput>) { |
| 46 | + const resolvedCacheConfig = cacheConfig ?? createCacheConfig() |
| 47 | + const resolvedQueryKeys = |
| 48 | + queryKeys ?? createAuthQueryKeys(queryKeyNamespace) |
| 49 | + |
| 50 | + function useAuth(options?: AuthQueryInput): UseAuthResult<TCustomer> { |
| 51 | + const { data, isLoading, isFetching, isSuccess, error } = useQuery({ |
| 52 | + queryKey: resolvedQueryKeys.customer(), |
| 53 | + queryFn: ({ signal }) => service.getCustomer(signal), |
| 54 | + enabled: options?.enabled ?? true, |
| 55 | + retry: false, |
| 56 | + ...resolvedCacheConfig.userData, |
| 57 | + }) |
| 58 | + |
| 59 | + const customer = data ?? null |
| 60 | + |
| 61 | + return { |
| 62 | + customer, |
| 63 | + isAuthenticated: customer !== null, |
| 64 | + isLoading, |
| 65 | + isFetching, |
| 66 | + isSuccess, |
| 67 | + error: |
| 68 | + error instanceof Error ? error.message : error ? String(error) : null, |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + function useSuspenseAuth(): UseSuspenseAuthResult<TCustomer> { |
| 73 | + const { data, isFetching } = useSuspenseQuery<TCustomer | null, Error>({ |
| 74 | + queryKey: resolvedQueryKeys.customer(), |
| 75 | + queryFn: ({ signal }) => service.getCustomer(signal), |
| 76 | + ...resolvedCacheConfig.userData, |
| 77 | + }) |
| 78 | + const customer = data ?? null |
| 79 | + |
| 80 | + return { |
| 81 | + customer, |
| 82 | + isAuthenticated: customer !== null, |
| 83 | + isFetching, |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + function useLogin(options?: AuthMutationOptions<unknown, TLoginInput>) { |
| 88 | + const queryClient = useQueryClient() |
| 89 | + return useMutation({ |
| 90 | + mutationFn: (input: TLoginInput) => service.login(input), |
| 91 | + onSuccess: (data, variables) => { |
| 92 | + queryClient.invalidateQueries({ |
| 93 | + queryKey: resolvedQueryKeys.customer(), |
| 94 | + }) |
| 95 | + options?.onSuccess?.(data, variables) |
| 96 | + }, |
| 97 | + onError: (error) => { |
| 98 | + options?.onError?.(error) |
| 99 | + }, |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + function useRegister(options?: AuthMutationOptions<unknown, TRegisterInput>) { |
| 104 | + const queryClient = useQueryClient() |
| 105 | + return useMutation({ |
| 106 | + mutationFn: (input: TRegisterInput) => service.register(input), |
| 107 | + onSuccess: (data, variables) => { |
| 108 | + queryClient.invalidateQueries({ |
| 109 | + queryKey: resolvedQueryKeys.customer(), |
| 110 | + }) |
| 111 | + options?.onSuccess?.(data, variables) |
| 112 | + }, |
| 113 | + onError: (error) => { |
| 114 | + options?.onError?.(error) |
| 115 | + }, |
| 116 | + }) |
| 117 | + } |
| 118 | + |
| 119 | + function useLogout(options?: AuthMutationOptions<void, void>) { |
| 120 | + const queryClient = useQueryClient() |
| 121 | + return useMutation({ |
| 122 | + mutationFn: () => service.logout(), |
| 123 | + onSuccess: () => { |
| 124 | + queryClient.setQueryData(resolvedQueryKeys.customer(), null) |
| 125 | + queryClient.invalidateQueries({ |
| 126 | + queryKey: resolvedQueryKeys.all(), |
| 127 | + }) |
| 128 | + queryClient.removeQueries({ |
| 129 | + queryKey: resolvedQueryKeys.all(), |
| 130 | + }) |
| 131 | + options?.onSuccess?.(undefined, undefined) |
| 132 | + }, |
| 133 | + onError: (error) => { |
| 134 | + options?.onError?.(error) |
| 135 | + }, |
| 136 | + }) |
| 137 | + } |
| 138 | + |
| 139 | + function useUpdateCustomer( |
| 140 | + options?: AuthMutationOptions<TCustomer, TUpdateInput> |
| 141 | + ) { |
| 142 | + const queryClient = useQueryClient() |
| 143 | + return useMutation({ |
| 144 | + mutationFn: (input: TUpdateInput) => { |
| 145 | + if (!service.updateCustomer) { |
| 146 | + throw new Error("updateCustomer service is not configured") |
| 147 | + } |
| 148 | + return service.updateCustomer(input) |
| 149 | + }, |
| 150 | + onSuccess: (data, variables) => { |
| 151 | + queryClient.setQueryData(resolvedQueryKeys.customer(), data) |
| 152 | + options?.onSuccess?.(data, variables) |
| 153 | + }, |
| 154 | + onError: (error) => { |
| 155 | + options?.onError?.(error) |
| 156 | + }, |
| 157 | + }) |
| 158 | + } |
| 159 | + |
| 160 | + function useRefreshAuth(options?: AuthMutationOptions<unknown, void>) { |
| 161 | + const queryClient = useQueryClient() |
| 162 | + return useMutation({ |
| 163 | + mutationFn: () => { |
| 164 | + if (!service.refresh) { |
| 165 | + throw new Error("refresh service is not configured") |
| 166 | + } |
| 167 | + return service.refresh() |
| 168 | + }, |
| 169 | + onSuccess: (data) => { |
| 170 | + queryClient.invalidateQueries({ |
| 171 | + queryKey: resolvedQueryKeys.customer(), |
| 172 | + }) |
| 173 | + options?.onSuccess?.(data, undefined) |
| 174 | + }, |
| 175 | + onError: (error) => { |
| 176 | + options?.onError?.(error) |
| 177 | + }, |
| 178 | + }) |
| 179 | + } |
| 180 | + |
| 181 | + return { |
| 182 | + useAuth, |
| 183 | + useSuspenseAuth, |
| 184 | + useLogin, |
| 185 | + useRegister, |
| 186 | + useLogout, |
| 187 | + useUpdateCustomer, |
| 188 | + useRefreshAuth, |
| 189 | + } |
| 190 | +} |
0 commit comments