Skip to content

Commit

Permalink
fix: allow useLogout without authModalContext
Browse files Browse the repository at this point in the history
`useLogout` does not depend on UI components and now will no longer
throw an error when used outside an `AuthModalProvider`.`
  • Loading branch information
dphilipson committed Sep 18, 2024
1 parent d786e35 commit a2ec3b4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion account-kit/react/src/components/auth/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const AuthModalContext = createContext<AuthContextType | undefined>(

// eslint-disable-next-line jsdoc/require-jsdoc
export const useAuthContext = (): AuthContextType => {
const context = useContext(AuthModalContext);
const context = useOptionalAuthContext();

if (!context) {
throw new Error(
Expand All @@ -37,3 +37,7 @@ export const useAuthContext = (): AuthContextType => {

return context;
};

// eslint-disable-next-line jsdoc/require-jsdoc
export const useOptionalAuthContext = (): AuthContextType | undefined =>
useContext(AuthModalContext);
6 changes: 3 additions & 3 deletions account-kit/react/src/hooks/useLogout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { disconnect } from "@account-kit/core";
import { useMutation, type UseMutateFunction } from "@tanstack/react-query";
import { useAuthContext } from "../components/auth/context.js";
import { useOptionalAuthContext } from "../components/auth/context.js";
import { useAlchemyAccountContext } from "../context.js";
import type { BaseHookMutationArgs } from "../types.js";

Expand Down Expand Up @@ -37,7 +37,7 @@ export function useLogout(
mutationArgs?: UseLogoutMutationArgs
): UseLogoutResult {
const { queryClient, config } = useAlchemyAccountContext();
const { resetAuthStep } = useAuthContext();
const authContext = useOptionalAuthContext();

const {
mutate: logout,
Expand All @@ -47,7 +47,7 @@ export function useLogout(
{
mutationFn: async () => {
await disconnect(config);
resetAuthStep();
authContext?.resetAuthStep();
},
...mutationArgs,
},
Expand Down

0 comments on commit a2ec3b4

Please sign in to comment.