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.
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
| Method | Parameters | Returns | Request | Login needed |
|---|---|---|---|---|
get | none | Promise<User | null> | GET auth/user | Yes (returns null without it) |
The result is the response data plus type: 'user'.
Queries
| Query | Key |
|---|---|
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
getnever throws. Any failure (guest, expired token, network error) is logged withconsole.debugand becomesnull, 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.