Skip to content

Commit

Permalink
fix: only reset auth state when using the logout hook
Browse files Browse the repository at this point in the history
  • Loading branch information
bswags committed Jul 12, 2024
1 parent cc706e1 commit 996c3a3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
1 change: 1 addition & 0 deletions account-kit/react/src/components/auth/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type AuthStep =
type AuthContextType = {
authStep: AuthStep;
setAuthStep: (step: AuthStep) => void;
resetAuthStep: () => void;
};

export const AuthModalContext = createContext<AuthContextType | undefined>(
Expand Down
27 changes: 14 additions & 13 deletions account-kit/react/src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ export const AlchemyAccountProvider = (
const openAuthModal = useCallback(() => setIsModalOpen(true), []);
const closeAuthModal = useCallback(() => setIsModalOpen(false), []);

/**
* Reset the auth step to the initial state. This also clears the email auth query params from the URL.
*/
const resetAuthStep = useCallback(() => {
setAuthStep({ type: "initial" });

const url = new URL(window.location.href);
url.searchParams.delete("orgId");
url.searchParams.delete("bundle");
url.searchParams.delete(IS_SIGNUP_QP);
window.history.replaceState({}, "", url.toString());
}, []);

const initialContext = useMemo(
() => ({
config,
Expand Down Expand Up @@ -151,19 +164,6 @@ export const AlchemyAccountProvider = (
}
}, [status, config.ui, openAuthModal]);

// Force the auth UI back to the initial state when disconnected
useEffect(() => {
if (status === "DISCONNECTED" && !isAuthenticating) {
setAuthStep({ type: "initial" });
// Remove the relevant query params (could also do this once connected)
const url = new URL(window.location.href);
url.searchParams.delete("orgId");
url.searchParams.delete("bundle");
url.searchParams.delete(IS_SIGNUP_QP);
window.history.replaceState({}, "", url.toString());
}
}, [status, isAuthenticating]);

return (
<Hydrate {...props}>
<AlchemyAccountContext.Provider value={initialContext}>
Expand All @@ -172,6 +172,7 @@ export const AlchemyAccountProvider = (
value={{
authStep,
setAuthStep,
resetAuthStep,
}}
>
{children}
Expand Down
5 changes: 4 additions & 1 deletion account-kit/react/src/hooks/useLogout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useDisconnect } from "wagmi";
import { useAlchemyAccountContext } from "../context.js";
import type { BaseHookMutationArgs } from "../types.js";
import { useSigner } from "./useSigner.js";
import { useAuthContext } from "../components/auth/context.js";

export type UseLogoutMutationArgs = BaseHookMutationArgs<void, void>;

Expand Down Expand Up @@ -44,6 +45,7 @@ export function useLogout(
} = useAlchemyAccountContext();
const signer = useSigner();
const { disconnectAsync } = useDisconnect({ config: wagmiConfig });
const { resetAuthStep } = useAuthContext();

const {
mutate: logout,
Expand All @@ -53,7 +55,8 @@ export function useLogout(
{
mutationFn: async () => {
await disconnectAsync();
return signer?.disconnect();
await signer?.disconnect();
resetAuthStep();
},
...mutationArgs,
},
Expand Down

0 comments on commit 996c3a3

Please sign in to comment.