Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// useMultiFactorResolverResolveSignInMutation (MultiFactorResolver)
// useApplyActionCodeMutation
// useCheckActionCodeMutation
// useConfirmPasswordResetMutation
export { useConfirmPasswordResetMutation } from "./useConfirmPasswordResetMutation";
// useCreateUserWithEmailAndPasswordMutation
// useFetchSignInMethodsForEmailQuery
// useGetRedirectResultQuery
Expand Down
119 changes: 119 additions & 0 deletions packages/react/src/auth/useConfirmPasswordResetMutation.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { act, renderHook, waitFor } from "@testing-library/react";
import {
createUserWithEmailAndPassword,
sendPasswordResetEmail,
} from "firebase/auth";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { auth, expectFirebaseError, wipeAuth } from "~/testing-utils";
import { useConfirmPasswordResetMutation } from "./useConfirmPasswordResetMutation";
import { waitForPasswordResetCode } from "./utils";
import { queryClient, wrapper } from "../../utils";

describe("useConfirmPasswordResetMutation", () => {
const email = "[email protected]";
const password = "TanstackQueryFirebase#123";
const newPassword = "NewSecurePassword#456";

beforeEach(async () => {
queryClient.clear();
await wipeAuth();
await createUserWithEmailAndPassword(auth, email, password);
});

afterEach(async () => {
vi.clearAllMocks();
await auth.signOut();
});

test("successfully resets password", async () => {
await sendPasswordResetEmail(auth, email);
const oobCode = await waitForPasswordResetCode(email);

const { result } = renderHook(() => useConfirmPasswordResetMutation(auth), {
wrapper,
});

await act(async () => {
await result.current.mutateAsync({ oobCode: oobCode!, newPassword });
});

await waitFor(() => expect(result.current.isSuccess).toBe(true));
});

test("handles invalid action code", async () => {
const invalidCode = "invalid-action-code";

const { result } = renderHook(() => useConfirmPasswordResetMutation(auth), {
wrapper,
});

await act(async () => {
try {
await result.current.mutateAsync({ oobCode: invalidCode, newPassword });
} catch (error) {
expectFirebaseError(error, "auth/invalid-action-code");
}
});

await waitFor(() => expect(result.current.isError).toBe(true));
expect(result.current.error).toBeDefined();
expectFirebaseError(result.current.error, "auth/invalid-action-code");
});

test("handles empty action code", async () => {
const { result } = renderHook(() => useConfirmPasswordResetMutation(auth), {
wrapper,
});

await act(async () => {
try {
await result.current.mutateAsync({ oobCode: "", newPassword });
} catch (error) {
expectFirebaseError(error, "auth/internal-error");
}
});

await waitFor(() => expect(result.current.isError).toBe(true));
expect(result.current.error).toBeDefined();
expectFirebaseError(result.current.error, "auth/internal-error");
});

test("executes onSuccess callback", async () => {
await sendPasswordResetEmail(auth, email);
const oobCode = await waitForPasswordResetCode(email);
const onSuccess = vi.fn();

const { result } = renderHook(
() => useConfirmPasswordResetMutation(auth, { onSuccess }),
{ wrapper }
);

await act(async () => {
await result.current.mutateAsync({ oobCode: oobCode!, newPassword });
});

await waitFor(() => expect(onSuccess).toHaveBeenCalled());
});

test("executes onError callback", async () => {
const invalidCode = "invalid-action-code";
const onError = vi.fn();

const { result } = renderHook(
() => useConfirmPasswordResetMutation(auth, { onError }),
{ wrapper }
);

await act(async () => {
try {
await result.current.mutateAsync({ oobCode: invalidCode, newPassword });
} catch (error) {
expectFirebaseError(error, "auth/invalid-action-code");
}
});

await waitFor(() => expect(onError).toHaveBeenCalled());
expect(onError.mock.calls[0][0]).toBeDefined();
expectFirebaseError(result.current.error, "auth/invalid-action-code");
});
});
26 changes: 26 additions & 0 deletions packages/react/src/auth/useConfirmPasswordResetMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { type UseMutationOptions, useMutation } from "@tanstack/react-query";
import { type Auth, type AuthError, confirmPasswordReset } from "firebase/auth";

type AuthUseMutationOptions<
TData = unknown,
TError = Error,
TVariables = void
> = Omit<UseMutationOptions<TData, TError, TVariables>, "mutationFn">;

export function useConfirmPasswordResetMutation(
auth: Auth,
options?: AuthUseMutationOptions<
void,
AuthError,
{ oobCode: string; newPassword: string }
>
) {
return useMutation<void, AuthError, { oobCode: string; newPassword: string }>(
{
...options,
mutationFn: ({ oobCode, newPassword }) => {
return confirmPasswordReset(auth, oobCode, newPassword);
},
}
);
}
Loading