Logging out a user ends their authenticated session, removing their access credentials from the device and requiring them to authenticate again to access protected resources.

logout: () => Promise<void>

Usage

To log a user out, use the logout method from the usePrivy hook:

import { usePrivy } from '@privy-io/react-auth';

function LogoutButton() {
  const { ready, authenticated, logout } = usePrivy();

  // Disable logout when Privy is not ready or the user is not authenticated
  const disableLogout = !ready || (ready && !authenticated);

  return (
    <button disabled={disableLogout} onClick={logout}>
      Log out
    </button>
  );
}

Callbacks

You can attach callbacks to the logout process using the useLogout hook:

import { useLogout } from '@privy-io/react-auth';

function LogoutButton() {
  const { logout } = useLogout({
    onSuccess: () => {
      console.log('User successfully logged out');
      // Redirect to landing page or perform other post-logout actions
    },
    onError: (error) => {
      console.error('Logout failed', error);
    }
  });

  return <button onClick={logout}>Log out</button>;
}

Was this page helpful?