Skip to content

feat(react/auth): add useUserGetIdTokenResultMutation #150

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

Open
wants to merge 3 commits 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
@@ -1,7 +1,7 @@
// useAuthStateReadyQuery (Auth)
// useConfirmationResultConfirmMutation (ConfirmationResult)
// useUserDeleteMutation (User)
// userUserGetIdTokenResultMutation (User)
export { useUserGetIdTokenResultMutation } from "./useUseGetIdTokenResultMutation";
// useUserGetIdTokenMutation (User)
// useUserReloadMutation (User)
// useVerifyPhoneNumberMutation (PhoneAuthProvider)
Expand Down
24 changes: 24 additions & 0 deletions packages/react/src/auth/useUseGetIdTokenResultMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { type UseMutationOptions, useMutation } from "@tanstack/react-query";
import {
type User,
type AuthError,
getIdTokenResult,
type IdTokenResult,
} from "firebase/auth";

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

export function useUserGetIdTokenResultMutation(
user: User,
options?: AuthUseMutationOptions<IdTokenResult, AuthError, boolean>
) {
return useMutation<IdTokenResult, AuthError, boolean>({
...options,
mutationFn: (forceRefresh?: boolean) =>
getIdTokenResult(user, forceRefresh),
});
}
132 changes: 132 additions & 0 deletions packages/react/src/auth/useUserGetIdTokenResultMutation.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { act, renderHook, waitFor } from "@testing-library/react";
import {
createUserWithEmailAndPassword,
signInWithEmailAndPassword,
} from "firebase/auth";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { auth, wipeAuth } from "~/testing-utils";
import { useUserGetIdTokenResultMutation } from "./useUseGetIdTokenResultMutation";
import { queryClient, wrapper } from "../../utils";

describe("useUserGetIdTokenResultMutation", () => {
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 retrieves ID token result with forceRefresh true", async () => {
const userCredential = await signInWithEmailAndPassword(
auth,
email,
password
);
const { user } = userCredential;

const { result } = renderHook(() => useUserGetIdTokenResultMutation(user), {
wrapper,
});

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

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

const tokenResult = result.current.data;

// Verify all IdTokenResult properties exist and have correct types
expect(tokenResult?.authTime).toBeTypeOf("string");
expect(tokenResult?.issuedAtTime).toBeTypeOf("string");
expect(tokenResult?.expirationTime).toBeTypeOf("string");
expect(tokenResult?.token).toBeTypeOf("string");
expect(tokenResult?.claims).toBeTypeOf("object");
expect(
typeof tokenResult?.signInProvider === "string" ||
tokenResult?.signInProvider === null
).toBe(true);
expect(
typeof tokenResult?.signInSecondFactor === "string" ||
tokenResult?.signInSecondFactor === null
).toBe(true);
});

test("can get token result with forceRefresh false", async () => {
const userCredential = await signInWithEmailAndPassword(
auth,
email,
password
);
const { user } = userCredential;

const { result } = renderHook(() => useUserGetIdTokenResultMutation(user), {
wrapper,
});

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

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

const tokenResult = result.current.data;
expect(tokenResult?.token).toBeTypeOf("string");
expect(tokenResult?.claims).toBeTypeOf("object");
});

test("executes onSuccess callback with token result", async () => {
const userCredential = await signInWithEmailAndPassword(
auth,
email,
password
);
const { user } = userCredential;
const onSuccess = vi.fn();

const { result } = renderHook(
() => useUserGetIdTokenResultMutation(user, { onSuccess }),
{ wrapper }
);

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

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

const tokenResult = onSuccess.mock.calls[0][0];
expect(tokenResult.token).toBeTypeOf("string");
expect(tokenResult.claims).toBeTypeOf("object");
expect(tokenResult.authTime).toBeTypeOf("string");
});

test("verifies signInProvider for password authentication", async () => {
const userCredential = await signInWithEmailAndPassword(
auth,
email,
password
);
const { user } = userCredential;

const { result } = renderHook(() => useUserGetIdTokenResultMutation(user), {
wrapper,
});

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

await waitFor(() => expect(result.current.isSuccess).toBe(true));
const tokenResult = result.current.data;
expect(tokenResult?.signInProvider).toBe("password");
expect(tokenResult?.signInSecondFactor).toBeNull();
});
});