Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react/auth): add useReauthenticateWithPhoneNumberMutation #170

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -45,7 +45,7 @@ export { useDeleteUserMutation } from "./useDeleteUserMutation";
// useLinkWithPhoneNumberMutation
// useLinkWithPopupMutation
// useLinkWithRedirectMutation
// useReauthenticateWithPhoneNumberMutation
export { useReauthenticateWithPhoneNumberMutation } from "./useReauthenticateWithPhoneNumberMutation";
// useReauthenticateWithCredentialMutation
// useReauthenticateWithPopupMutation
// useReauthenticateWithRedirectMutation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { renderHook, waitFor } from "@testing-library/react";
import {
reauthenticateWithPhoneNumber,
type User,
type ApplicationVerifier,
type ConfirmationResult,
} from "firebase/auth";
import { useReauthenticateWithPhoneNumberMutation } from "./useReauthenticateWithPhoneNumberMutation";
import { describe, test, expect, beforeEach, vi } from "vitest";
import { wrapper, queryClient } from "../../utils";

vi.mock("firebase/auth", async () => {
const actual = await vi.importActual<typeof import("firebase/auth")>(
"firebase/auth"
);
return {
...actual,
reauthenticateWithPhoneNumber: vi.fn(),
};
});

describe("useReauthenticateWithPhoneNumberMutation", () => {
let user: User;
let appVerifier: ApplicationVerifier;

const phoneNumber = "+16505550101";

beforeEach(async () => {
queryClient.clear();
vi.resetAllMocks();
user = { uid: "test-user" } as User;
appVerifier = { type: "recaptcha" } as ApplicationVerifier;
});

test("should successfully reauthenticate with correct phone number", async () => {
const confirmationResult = {
verificationId: "123456",
} as ConfirmationResult;
vi.mocked(reauthenticateWithPhoneNumber).mockResolvedValue(
confirmationResult
);

const { result } = renderHook(
() => useReauthenticateWithPhoneNumberMutation(appVerifier),
{ wrapper }
);

result.current.mutate({ user, phoneNumber });

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

test("should fail with incorrect phone number", async () => {
vi.mocked(reauthenticateWithPhoneNumber).mockRejectedValue({
code: "auth/invalid-phone-number",
});

const { result } = renderHook(
() => useReauthenticateWithPhoneNumberMutation(appVerifier),
{ wrapper }
);

result.current.mutate({ user, phoneNumber: "+123" });

await waitFor(() => {
expect(result.current.isError).toBe(true);
expect(result.current.error?.code).toBe("auth/invalid-phone-number");
});
});

test("should call onSuccess callback after successful reauthentication", async () => {
let callbackCalled = false;
const confirmationResult = {
verificationId: "123456",
} as ConfirmationResult;

vi.mocked(reauthenticateWithPhoneNumber).mockResolvedValue(
confirmationResult
);

const { result } = renderHook(
() =>
useReauthenticateWithPhoneNumberMutation(appVerifier, {
onSuccess: () => {
callbackCalled = true;
},
}),
{ wrapper }
);

result.current.mutate({ user, phoneNumber });

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

test("should call onError callback on authentication failure", async () => {
let errorCode: string | undefined;

vi.mocked(reauthenticateWithPhoneNumber).mockRejectedValue({
code: "auth/invalid-verification-code",
});

const { result } = renderHook(
() =>
useReauthenticateWithPhoneNumberMutation(appVerifier, {
onError: (error) => {
errorCode = error.code;
},
}),
{ wrapper }
);

result.current.mutate({ user, phoneNumber });

await waitFor(() => {
expect(result.current.isError).toBe(true);
expect(errorCode).toBe("auth/invalid-verification-code");
});
});

test("should handle multiple reauthentication attempts", async () => {
const confirmationResult = {
verificationId: "123456",
} as ConfirmationResult;

vi.mocked(reauthenticateWithPhoneNumber).mockResolvedValue(
confirmationResult
);

const { result } = renderHook(
() => useReauthenticateWithPhoneNumberMutation(appVerifier),
{ wrapper }
);

// First attempt
result.current.mutate({ user, phoneNumber });

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

// Second attempt
result.current.mutate({ user, phoneNumber });

await waitFor(() => {
expect(result.current.isSuccess).toBe(true);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useMutation, type UseMutationOptions } from "@tanstack/react-query";
import {
type AuthError,
type User,
type ApplicationVerifier,
type ConfirmationResult,
reauthenticateWithPhoneNumber,
} from "firebase/auth";

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

export function useReauthenticateWithPhoneNumberMutation(
appVerifier: ApplicationVerifier,
options?: AuthMutationOptions<
ConfirmationResult,
AuthError,
{
user: User;
phoneNumber: string;
}
>
) {
return useMutation<
ConfirmationResult,
AuthError,
{
user: User;
phoneNumber: string;
}
>({
...options,
mutationFn: ({ user, phoneNumber }) =>
reauthenticateWithPhoneNumber(user, phoneNumber, appVerifier),
});
}