From 9334cfdb15783fb14b62aaf31a7972a48deb5437 Mon Sep 17 00:00:00 2001 From: HassanBahati Date: Thu, 10 Oct 2024 13:42:45 +0300 Subject: [PATCH 1/2] feat(useRevokeAccessTokenMutation): add useRevokeAccessTokenMutation hook --- packages/react/src/auth/index.ts | 3 +- .../useRevokeAccessTokenMutation.test.tsx | 45 +++++++++++++++++++ .../src/auth/useRevokeAccessTokenMutation.ts | 22 +++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 packages/react/src/auth/useRevokeAccessTokenMutation.test.tsx create mode 100644 packages/react/src/auth/useRevokeAccessTokenMutation.ts diff --git a/packages/react/src/auth/index.ts b/packages/react/src/auth/index.ts index 632083b7..7ae03693 100644 --- a/packages/react/src/auth/index.ts +++ b/packages/react/src/auth/index.ts @@ -15,7 +15,7 @@ // useCreateUserWithEmailAndPasswordMutation // useFetchSignInMethodsForEmailQuery // useGetRedirectResultQuery -// useRevokeAccessTokenMutation +export { useRevokeAccessTokenMutation } from "./useRevokeAccessTokenMutation"; // useSendPasswordResetEmailMutation // useSendSignInLinkToEmailMutation export { useSignInAnonymouslyMutation } from "./useSignInAnonymouslyMutation"; @@ -46,4 +46,3 @@ export { useSignInAnonymouslyMutation } from "./useSignInAnonymouslyMutation"; // useUpdatePasswordMutation // useUpdateProfileMutation // useVerifyBeforeUpdateEmailMutation - diff --git a/packages/react/src/auth/useRevokeAccessTokenMutation.test.tsx b/packages/react/src/auth/useRevokeAccessTokenMutation.test.tsx new file mode 100644 index 00000000..13caec61 --- /dev/null +++ b/packages/react/src/auth/useRevokeAccessTokenMutation.test.tsx @@ -0,0 +1,45 @@ +import React from "react"; +import { describe, expect, test, beforeEach, afterEach } from "vitest"; +import { renderHook, act, waitFor } from "@testing-library/react"; +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { useRevokeAccessTokenMutation } from "./useRevokeAccessTokenMutation"; +import { + OAuthProvider, + signInWithPopup, + type UserCredential, +} from "firebase/auth"; +import { auth, wipeAuth } from "~/testing-utils"; + +const queryClient = new QueryClient({ + defaultOptions: { + queries: { retry: false }, + mutations: { retry: false }, + }, +}); + +const wrapper = ({ children }: { children: React.ReactNode }) => ( + {children} +); + +describe("useRevokeAccessTokenMutation", () => { + let userCredential: UserCredential; + + beforeEach(async () => { + const provider = new OAuthProvider("apple.com"); + userCredential = await signInWithPopup(auth, provider); + }); + + afterEach(async () => { + queryClient.clear(); + await auth.signOut(); + await wipeAuth(); + }); + + test("successfully revokes access token", async () => { + const { result } = renderHook(() => useRevokeAccessTokenMutation(auth), { + wrapper, + }); + + const oauthAccessToken = await userCredential.user.getIdToken(); + }); +}); diff --git a/packages/react/src/auth/useRevokeAccessTokenMutation.ts b/packages/react/src/auth/useRevokeAccessTokenMutation.ts new file mode 100644 index 00000000..bb466561 --- /dev/null +++ b/packages/react/src/auth/useRevokeAccessTokenMutation.ts @@ -0,0 +1,22 @@ +import { useMutation, type UseMutationOptions } from "@tanstack/react-query"; +import { type Auth, AuthError, revokeAccessToken } from "firebase/auth"; + +type RevokeAccessTokenParams = { + token: string; +}; + +type AuthUseMutationOptions< + TData = unknown, + TError = Error, + TVariables = void +> = Omit, "mutationFn">; + +export function useRevokeAccessTokenMutation( + auth: Auth, + options?: AuthUseMutationOptions +) { + return useMutation({ + ...options, + mutationFn: ({ token }) => revokeAccessToken(auth, token), + }); +} From 886ea821639691a1bd638fb735ceb437678096e0 Mon Sep 17 00:00:00 2001 From: HassanBahati Date: Thu, 10 Oct 2024 13:45:49 +0300 Subject: [PATCH 2/2] _ --- .../react/src/auth/useRevokeAccessTokenMutation.test.tsx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/react/src/auth/useRevokeAccessTokenMutation.test.tsx b/packages/react/src/auth/useRevokeAccessTokenMutation.test.tsx index 13caec61..aac3d1ea 100644 --- a/packages/react/src/auth/useRevokeAccessTokenMutation.test.tsx +++ b/packages/react/src/auth/useRevokeAccessTokenMutation.test.tsx @@ -41,5 +41,11 @@ describe("useRevokeAccessTokenMutation", () => { }); const oauthAccessToken = await userCredential.user.getIdToken(); + + act(() => { + result.current.mutate({ token: oauthAccessToken }); + }); + + await waitFor(() => expect(result.current.isSuccess).toBe(true)); }); });