diff --git a/packages/react/src/auth/index.ts b/packages/react/src/auth/index.ts index 2452033..d9b9cdb 100644 --- a/packages/react/src/auth/index.ts +++ b/packages/react/src/auth/index.ts @@ -28,7 +28,7 @@ export { useSignInAnonymouslyMutation } from "./useSignInAnonymouslyMutation"; // useSignInWithRedirectMutation // useSignOutMutation // useUpdateCurrentUserMutation -// useValidatePasswordMutation +export { useValidatePasswordMutation } from "./useValidatePasswordMutation"; // useVerifyPasswordResetCodeMutation // useDeleteUserMutation // useLinkWithCredentialMutation diff --git a/packages/react/src/auth/useValidatePasswordMutation.test.tsx b/packages/react/src/auth/useValidatePasswordMutation.test.tsx new file mode 100644 index 0000000..fb66569 --- /dev/null +++ b/packages/react/src/auth/useValidatePasswordMutation.test.tsx @@ -0,0 +1,37 @@ +import React from "react"; +import { describe, expect, test, beforeEach, afterEach } from "vitest"; +import { renderHook, act, waitFor } from "@testing-library/react"; +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { useValidatePasswordMutation } from "./useValidatePasswordMutation"; +import { auth, wipeAuth } from "~/testing-utils"; + +const queryClient = new QueryClient({ + defaultOptions: { + queries: { retry: false }, + mutations: { retry: false }, + }, +}); + +const wrapper = ({ children }: { children: React.ReactNode }) => ( + {children} +); + +describe("useValidatePasswordMutation", () => { + const validPassword = "ValidPassword123!"; + const invalidPassword = "short"; + + beforeEach(async () => { + queryClient.clear(); + await wipeAuth(); + }); + + test("validates a password successfully", async () => { + const { result } = renderHook(() => useValidatePasswordMutation(auth), { + wrapper, + }); + + act(() => { + result.current.mutate(validPassword); + }); + }); +}); diff --git a/packages/react/src/auth/useValidatePasswordMutation.ts b/packages/react/src/auth/useValidatePasswordMutation.ts new file mode 100644 index 0000000..d90ec36 --- /dev/null +++ b/packages/react/src/auth/useValidatePasswordMutation.ts @@ -0,0 +1,23 @@ +import { useMutation, type UseMutationOptions } from "@tanstack/react-query"; +import { + type Auth, + type AuthError, + type PasswordValidationStatus, + validatePassword, +} from "firebase/auth"; + +type AuthUseMutationOptions< + TData = unknown, + TError = Error, + TVariables = void +> = Omit, "mutationFn">; + +export function useValidatePasswordMutation( + auth: Auth, + options?: AuthUseMutationOptions +) { + return useMutation({ + ...options, + mutationFn: (password: string) => validatePassword(auth, password), + }); +}