useUser
The logged-in customer as a TanStack Query result, plus an isLoggedIn flag; it never requests anything for guests.
import { useUser, UseUserReturn } from '@salla.sa/twilight-theme-engine/hooks/useUser';In plain words
Tells you who is shopping. For a guest, isLoggedIn is false and data is null. For a logged-in customer, data becomes their profile (first name, email, avatar, preferences…) once it has loaded.
The result is a TanStack Query result, the library the engine uses to load and cache data, so it also carries isFetching, refetch and the other query fields.
Signature
function useUser(): UseUserReturn
type UseUserReturn = UseQueryResult<User | null> & {
isLoggedIn: boolean; // an auth token exists
};Try it live
Hello, guest
useUser(): {…} 5 keys
import { useUser } from '@salla.sa/twilight-theme-engine/hooks/useUser';
export function Greeting() {
const { data: user, isLoggedIn } = useUser();
if (!isLoggedIn) return <p>Hello, guest</p>;
if (!user) return <p>Loading your profile…</p>; // or an expired session: see Gotchas
return <p>Welcome back, {user.first_name}</p>;
}
Example
import { useUser } from '@salla.sa/twilight-theme-engine/hooks/useUser';
export function Greeting() {
const { data: user, isLoggedIn, isFetching } = useUser();
if (!isLoggedIn) return <span>Hello, guest</span>;
if (!user) return <span>{isFetching ? 'Loading…' : 'Your session has expired'}</span>;
return <span>Hello, {user.first_name}</span>;
}
How it behaves
It is
useQuery({ ...user.queries.current(), enabled: !!token, placeholderData: null }), with the query key['user', 'current']and the endpointauth/user. Invalidate that key after a login, logout or profile change.The token comes from the engine's request context (
getTwilightContext().authToken) while rendering, so it works inside the engine runtime a theme runs in, not in an isolated unit test without that context.Every query field is passed through:
status,fetchStatus,refetch,isPlaceholderData,dataUpdatedAt…useGtmand the default Sift handler onbody:startcall it internally.
Gotchas
isLoggedInonly means a token exists. The request function catches every error and resolvesnull, so an expired or invalid token givesisLoggedIn: true,data: nullandisError: false. Treat that combination as "logged out".UseUserReturnis exported only from@salla.sa/twilight-theme-engine/hooks/useUser; the@salla.sa/twilight-theme-engine/hooksbarrel exports justuseUser.