Skip to content

useLogout

The useLogout hook logs a user out of their Embedded Account on your application.

WARNING

This requires your user to be logged in. See useAuthenticate for more details.

Import

ts
import { useLogout } from "@alchemy/aa-alchemy/react";

Usage

tsx
import { useLogout } from "@alchemy/aa-alchemy/react";

export function ComponentWithLogout() {
  /**
   * Assumes the app has context of a signer with an authenticated user
   * by using the `AlchemyAccountProvider` from `@alchemy/aa-alchemy/react`.
   */
  const { logout } = useLogout({
    onSuccess: () => {
      // [optional] Do something after the user has been logged out
    },
    onError: (error) => {
      // [optional] Do something with the error
    },
    // [optional] ...additional mutationArgs
  });

  return (
    <div>
      <button onClick={() => logout}>Logout</button>
    </div>
  );
}

Params

You can optionally specify any of the React Query useMutation parameters as parameters to this hook.

Return Type

ts
import { type useLogoutResult } from "@alchemy/aa-alchemy/react";

Returns an object containing the following state.

logout

UseMutateFunction A React query mutation function to log the user out.

isLoggingOut

boolean A flag that determines whether the mutation is still running or not.

error

Error | null A field that relays any errors from the mutation. It is null if there is no error.