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 useReauthenticateWithRedirectMutation #168

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 @@ -48,7 +48,7 @@ export { useDeleteUserMutation } from "./useDeleteUserMutation";
// useReauthenticateWithPhoneNumberMutation
// useReauthenticateWithCredentialMutation
// useReauthenticateWithPopupMutation
// useReauthenticateWithRedirectMutation
export { useReauthenticateWithRedirectMutation } from "./useReauthenticateWithRedirectMutation";
export { useReloadMutation } from "./useReloadMutation";
// useSendEmailVerificationMutation
// useUnlinkMutation
Expand Down
151 changes: 151 additions & 0 deletions packages/react/src/auth/useReauthenticateWithRedirectMutation.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { renderHook, waitFor } from "@testing-library/react";
import {
createUserWithEmailAndPassword,
signOut,
GoogleAuthProvider,
type User,
reauthenticateWithRedirect,
AuthError,
} from "firebase/auth";
import { useReauthenticateWithRedirectMutation } from "./useReauthenticateWithRedirectMutation";
import { describe, test, expect, beforeEach, afterEach, vi } from "vitest";
import { wrapper, queryClient } from "../../utils";
import { auth, wipeAuth } from "~/testing-utils";

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

describe("useReauthenticateWithRedirectMutation", () => {
let user: User;

const email = "[email protected]";
const password = "TanstackQueryFirebase#123";

const mockUser = { uid: "test-uid", email: "[email protected]" } as User;

beforeEach(async () => {
queryClient.clear();
await wipeAuth();
vi.clearAllMocks();
const userCredential = await createUserWithEmailAndPassword(
auth,
email,
password
);
user = userCredential.user;
});

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

test("should initiate redirect reauthentication", async () => {
const provider = new GoogleAuthProvider();

const { result } = renderHook(
() => useReauthenticateWithRedirectMutation(provider),
{ wrapper }
);

result.current.mutate({
user: mockUser,
});

await waitFor(() => {
expect(reauthenticateWithRedirect).toHaveBeenCalledWith(
mockUser,
provider,
undefined
);
});
});

test("should handle reauthentication error", async () => {
const provider = new GoogleAuthProvider();
const mockError: Partial<AuthError> = {
code: "auth/operation-not-allowed",
message: "Operation not allowed",
name: "AuthError",
};

vi.mocked(reauthenticateWithRedirect).mockRejectedValueOnce(mockError);

const { result } = renderHook(
() => useReauthenticateWithRedirectMutation(provider),
{ wrapper }
);

result.current.mutate({
user: mockUser,
});

await waitFor(() => {
expect(result.current.isError).toBe(true);
expect(result.current.error).toBe(mockError);
});
});

test("should call lifecycle hooks in correct order", async () => {
const provider = new GoogleAuthProvider();
const lifecycleCalls: string[] = [];

const { result } = renderHook(
() =>
useReauthenticateWithRedirectMutation(provider, {
onSettled: () => {
lifecycleCalls.push("onSettled");
},
onSuccess: () => {
lifecycleCalls.push("onSuccess");
},
onError: () => {
lifecycleCalls.push("onError");
},
}),
{ wrapper }
);

result.current.mutate({
user: mockUser,
});

await waitFor(() => {
expect(result.current.isSuccess).toBe(true);
expect(lifecycleCalls).toEqual(["onSuccess", "onSettled"]);
});
});

test("should handle optional resolver parameter", async () => {
const provider = new GoogleAuthProvider();
const mockResolver = {
_redirectPersistence: "mock",
_redirectUri: "mock",
};

const { result } = renderHook(
() => useReauthenticateWithRedirectMutation(provider),
{ wrapper }
);

result.current.mutate({
user: mockUser,
resolver: mockResolver,
});

await waitFor(() => {
expect(reauthenticateWithRedirect).toHaveBeenCalledWith(
mockUser,
provider,
mockResolver
);
});
});
});
39 changes: 39 additions & 0 deletions packages/react/src/auth/useReauthenticateWithRedirectMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useMutation, type UseMutationOptions } from "@tanstack/react-query";
import {
type AuthProvider,
reauthenticateWithRedirect,
type AuthError,
type User,
type PopupRedirectResolver,
} from "firebase/auth";

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

export function useReauthenticateWithRedirectMutation(
provider: AuthProvider,
options?: AuthMutationOptions<
never,
AuthError,
{
user: User;
resolver?: PopupRedirectResolver;
}
>
) {
return useMutation<
never,
AuthError,
{
user: User;
resolver?: PopupRedirectResolver;
}
>({
...options,
mutationFn: ({ user, resolver }) =>
reauthenticateWithRedirect(user, provider, resolver),
});
}