Skip to content
On this page

useUser

Definition

Composable for user management.

Basic usage

ts
const { 
 user,
 isLoggedIn,
 isCustomerSession,
 isGuestSession,
 country,
 salutation,
 defaultBillingAddressId,
 defaultShippingAddressId,
 userDefaultPaymentMethod,
 userDefaultBillingAddress,
 userDefaultShippingAddress,
 login,
 register,
 refreshUser,
 logout,
 loadCountry,
 loadSalutation,
 updatePersonalInfo,
 updateEmail,
 setDefaultPaymentMethod 
} = useUser();

Signature

ts
export function useUser(): UseUserReturn 

Return type

See UseUserReturn
ts
export type UseUserReturn = {
  /**
   * Logs-in user with given credentials
   * @param params - username and password
   *
   * @see https://github.com/shopware/frontends/issues/112 if login fails due to missing context token
   */
  login(params: { username: string; password: string }): Promise<void>;
  /**
   * Registers the user for given credentials
   * @param params {@link CustomerRegistrationParams}
   * @returns {@link Customer} object on success
   */
  register(params: CustomerRegistrationParams): Promise<Customer>;
  /**
   * Whole {@link Customer} object
   */
  user: ComputedRef<Partial<Customer> | undefined>;
  /**
   * Indicates if the user is logged in
   */
  isLoggedIn: ComputedRef<boolean>;
  /**
   * Indicates if the user is logged in as a customer (not a guest)
   */
  isCustomerSession: ComputedRef<boolean>;
  /**
   * Indicates if the user is logged in as a guest
   */
  isGuestSession: ComputedRef<boolean>;
  /**
   * {@link Country} of the user
   */
  country: Ref<Country | null>;
  /**
   * {@link Salutation} of the user
   */
  salutation: Ref<Salutation | null>;
  /**
   * Default billing address id
   */
  defaultBillingAddressId: ComputedRef<string | null>;
  /**
   * Default shipping address id
   */
  defaultShippingAddressId: ComputedRef<string | null>;
  /**
   * Fetches the user data from the API
   */
  refreshUser(): Promise<void>;
  /**
   * Logs out the user
   */
  logout(): Promise<void>;
  /**
   * Loads the {@link Country} of the user
   */
  loadCountry(countryId: string): Promise<void>;
  /**
   * Loads the {@link Salutation} for given id
   */
  loadSalutation(salutationId: string): Promise<void>;
  /**
   * Updates the user profile data
   * @param personals {@link CustomerUpdateProfileParam}
   * @returns
   */
  updatePersonalInfo(personals: CustomerUpdateProfileParam): Promise<void>;
  /**
   * Updates the user email
   * @param updateEmailData - {@link CustomerUpdateEmailParam}
   * @returns
   */
  updateEmail(updateEmailData: CustomerUpdateEmailParam): Promise<void>;
  /**
   * Sets the default payment method for given id
   * @param paymentMethodId
   * @returns
   */
  setDefaultPaymentMethod(paymentMethodId: string): Promise<void>;
  /**
   * Default payment method for the user
   */
  userDefaultPaymentMethod: ComputedRef<PaymentMethodTranslation | null>;
  /**
   * Default billing address for the user
   */
  userDefaultBillingAddress: ComputedRef<BillingAddress | null>;
  /**
   * Default shipping address for the user
   */
  userDefaultShippingAddress: ComputedRef<ShippingAddress | null>;
};

Properties

NameTypeDescription
user
ComputedRef<Partial<Customer> | undefined>
Whole {@link Customer} object
isLoggedIn
ComputedRef<boolean>
Indicates if the user is logged in
isCustomerSession
ComputedRef<boolean>
Indicates if the user is logged in as a customer (not a guest)
isGuestSession
ComputedRef<boolean>
Indicates if the user is logged in as a guest
country
Ref<Country | null>
{@link Country} of the user
salutation
Ref<Salutation | null>
{@link Salutation} of the user
defaultBillingAddressId
ComputedRef<string | null>
Default billing address id
defaultShippingAddressId
ComputedRef<string | null>
Default shipping address id
userDefaultPaymentMethod
ComputedRef<PaymentMethodTranslation | null>
Default payment method for the user
userDefaultBillingAddress
ComputedRef<BillingAddress | null>
Default billing address for the user
userDefaultShippingAddress
ComputedRef<ShippingAddress | null>
Default shipping address for the user

Methods

NameTypeDescription
login
Promise<void>
Logs-in user with given credentials
register
Promise<Customer>
Registers the user for given credentials
refreshUser
Promise<void>
Fetches the user data from the API
logout
Promise<void>
Logs out the user
loadCountry
Promise<void>
Loads the {@link Country} of the user
loadSalutation
Promise<void>
Loads the {@link Salutation} for given id
updatePersonalInfo
Promise<void>
Updates the user profile data
updateEmail
Promise<void>
Updates the user email
setDefaultPaymentMethod
Promise<void>
Sets the default payment method for given id
useUser has loaded