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: 2 additions & 0 deletions packages/react/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// useMultiFactorUserUnenrollMutation (MultiFactorUser)
// useMultiFactorUserGetSessionMutation (MultiFactorUser)
// useMultiFactorResolverResolveSignInMutation (MultiFactorResolver)
// useApplyActionCodeMutation
export { useCheckActionCodeMutation } from "./useCheckActionCodeMutation";
export { useApplyActionCodeMutation } from "./useApplyActionCodeMutation";
// useCheckActionCodeMutation
// useConfirmPasswordResetMutation
Expand Down
135 changes: 135 additions & 0 deletions packages/react/src/auth/useCheckActionCodeMutation.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { act, renderHook, waitFor } from "@testing-library/react";
import {
createUserWithEmailAndPassword,
sendPasswordResetEmail,
type ActionCodeInfo,
} from "firebase/auth";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { auth, expectFirebaseError, wipeAuth } from "~/testing-utils";
import { useCheckActionCodeMutation } from "./useCheckActionCodeMutation";
import { waitForPasswordResetCode } from "./utils";
import { queryClient, wrapper } from "../../utils";

describe("useCheckActionCodeMutation", () => {
const email = "[email protected]";
const password = "TanstackQueryFirebase#123";

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

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

test("successfully checks password reset action code", async () => {
await sendPasswordResetEmail(auth, email);
const oobCode = await waitForPasswordResetCode(email);

if (!oobCode) {
throw new Error("oobCode is null");
}

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

await act(async () => {
await result.current.mutateAsync(oobCode);
});

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

const actionCodeInfo = result.current.data as ActionCodeInfo;
expect(actionCodeInfo.operation).toBe("PASSWORD_RESET");
expect(actionCodeInfo.data.email).toBe(email);
});

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

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

await act(async () => {
try {
await result.current.mutateAsync(invalidCode);
} 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(() => useCheckActionCodeMutation(auth), {
wrapper,
});

await act(async () => {
try {
await result.current.mutateAsync("");
} 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();

if (!oobCode) {
throw new Error("oobCode is null");
}

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

await act(async () => {
await result.current.mutateAsync(oobCode);
});

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

const actionCodeInfo = onSuccess.mock.calls[0][0] as ActionCodeInfo;
expect(actionCodeInfo.operation).toBe("PASSWORD_RESET");
expect(actionCodeInfo.data.email).toBe(email);
});

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

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

await act(async () => {
try {
await result.current.mutateAsync(invalidCode);
} 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");
});
});
23 changes: 23 additions & 0 deletions packages/react/src/auth/useCheckActionCodeMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { type UseMutationOptions, useMutation } from "@tanstack/react-query";
import {
type Auth,
type ActionCodeInfo,
checkActionCode,
type AuthError,
} from "firebase/auth";

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

export function useCheckActionCodeMutation(
auth: Auth,
options?: AuthUseMutationOptions<ActionCodeInfo, AuthError, string>
) {
return useMutation<ActionCodeInfo, AuthError, string>({
...options,
mutationFn: (oobCode) => checkActionCode(auth, oobCode),
});
}