user

Read the signed-in customer's profile, or get null for a guest.

Use user to find out who the customer is. Most of the time you want the useUser() hook, which runs this query only when a token exists and returns isLoggedIn beside the result.

app/components/Greeting.tsx
import { useQuery } from '@tanstack/react-query';
import { useUser } from '@salla.sa/twilight-theme-engine/hooks/useUser';
import { user } from '@salla.sa/twilight-theme-engine/api/user';

export function Greeting() {
  const { isLoggedIn } = useUser();
  const { data: me } = useQuery({
    ...user.queries.current(),
    enabled: isLoggedIn,
  });

  return <p>{me ? `Hello ${me.first_name}` : 'Hello'}</p>;
}

Editing the profile is in profile.

Methods

MethodParametersReturnsRequestLogin needed
getnonePromise<User | null>GET auth/userYes (returns null without it)

The result is the response data plus type: 'user'.

Queries

QueryKey
user.queries.current()['user', 'current']

The key includes neither the locale nor the scope, and it is the same for every customer. Reset it when the customer signs in or out: queryClient.invalidateQueries({ queryKey: user.queries.current().queryKey }).

Gotchas

  • get never throws. Any failure (guest, expired token, network error) is logged with console.debug and becomes null, so you cannot tell "guest" from "request failed" by the result.
  • Because errors are swallowed, the query never enters an error state and does not retry.
  • After sign-out, clear this key and also ['orders'] and ['wishlist'], so a cached customer does not linger.

See also

On this page