useAuth hook

The useAuth hook can be used to determine whether a customer is logged in, as well as to log them in or out.

It returns an object containing:

loggedIn: boolean Whether or not the user is logged in to an account.

logOut(): void Logs the user out, clearing their session and resetting the Apollo cache.

logIn(token: string, customerId?: string): void Logs the user in, setting their session token in a cookie. The token argument should contain the same token returned from the API's login Mutation. Pass returned customerId if you want to merge current anonymous cart with the identified customer.

Note that in most cases you will not need to use logIn yourself - this is handled by the LoginFormProvider component.

Example usage:

Copy
Copied
const { loggedIn, logOut, logIn } = useAuth();

function logInOrOut(token) {
  if (loggedIn) {
    logOut();
  } else {
    logIn(token);
  }
}

return (
  <button onClick={() => logInOrOut(token)}>
    {loggedIn ? 'Log out' : 'Log in'}
  </button>
);
Copyright © Norce 2023. All right reserved.