User
Describes the signed-in customer's profile: name, email, mobile, avatar, preferences and linked social accounts.
import { User, UserPreferences, UserSocialAccount } from '@salla.sa/twilight-theme-engine/types';In plain words
User is the shape of a customer's profile once they have signed in: first and last name, email, mobile number, avatar, and their preferred language and currency.
useUser() gives you one in data. For a guest (a shopper who has not signed in) data is null, so check for that before reading any field.
Signature
interface User {
type: 'guest' | 'user';
id?: number;
first_name?: string; last_name?: string;
email?: string; mobile?: string; country_code?: string;
avatar?: string; gender?: string; birthday?: string | null;
created_at?: number;
preferences?: UserPreferences;
social_accounts?: UserSocialAccount[];
}
interface UserPreferences {
currency_code: string; language_code: string;
name_visible: boolean; notifications_enabled: boolean;
}
interface UserSocialAccount { provider_id: string; provider: string; linked: boolean }Example
import type { User } from '@salla.sa/twilight-theme-engine/types';
import { useUser } from '@salla.sa/twilight-theme-engine/hooks/useUser';
function displayName(user: User): string {
return [user.first_name, user.last_name].filter(Boolean).join(' ') || user.email || '';
}
export function AccountGreeting() {
const { data: user } = useUser();
if (!user) return null; // a guest, or the profile has not loaded yet
return <span className="account-greeting">Hello, {displayName(user)}</span>;
}
How it behaves
useUser()(@salla.sa/twilight-theme-engine/hooks/useUser) anduser.queries.current()(@salla.sa/twilight-theme-engine/api/user) giveUser | null.user.get()callsauth/userand addstype: "user"to what the API sends; any failure becomesnull.Every profile field is optional, even for a signed-in customer: the type promises only
type.The playground never signs in, so no demo here reads a real profile. The useUser demo shows the guest case.
Gotchas
A guest is
null, never{ type: "guest" }: nothing in the engine builds that value.if (user.type === "guest")never runs, and whenuserisnullit throws. Testuser == null, or readisLoggedInfromuseUser().docs/07-data-types.md lists
name,languageandcan_access_walletonUser. They are not in the type, souser.namedoes not compile. Joinfirst_nameandlast_name.
Related
The logged-in customer as a TanStack Query result, plus an isLoggedIn flag; it never requests anything for guests.
userFetches the signed-in customer's profile; for a guest, or after any failure, it resolves to null instead of throwing.
profileTwo writes on the signed-in customer's account: save one on/off preference, or delete the account.