Skip To Main Content

useLogout

useLogout is a React hook that facilitates logout from your Faust app.

API

The useLogout exports, defined as a TypeScript type:

type UseLogout = {
  loading: boolean;
  error: Response | undefined;
  logout: (redirectUrl?: string) => void;
};
Code language: JavaScript (javascript)

Loading: If true, the request is still in flight.
Error: If the logout process produces one or more errors, this object contains either an array of graphQLErrors or a single networkError. Otherwise, this value is undefined.
Logout: Callback function to initiate the logout request. You can pass an optional redirectUrl parameter that is used to redirect once you successfully complete the process.
useLogout: response reference.

Usage

Below is an example of displaying a logout button, and upon successful logout, redirecting the user back to the homepage:

import { useLogout } from '@faustwp/core';

export function AuthenticatedView() {
  const { logout } = useLogout();

  return (
    <>
      <button onClick={() => logout('/')}>Logout</button>
    </>
  );
}
Code language: JavaScript (javascript)