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: Omit<
      operations["register post /account/register"]["body"],
      "storefrontUrl"
    >,
  ): Promise<Schemas["Customer"]>;
  /**
   * Whole {@link Customer} object
   */
  user: ComputedRef<Schemas["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<Schemas["Country"] | null>;
  /**
   * {@link Salutation} of the user
   */
  salutation: Ref<Schemas["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 RequestParameters<'changeProfile'>}
   * @returns
   */
  updatePersonalInfo(
    personals: operations["changeProfile post /account/change-profile"]["body"],
  ): Promise<void>;
  /**
   * Updates the user email
   * @param updateEmailData - {@link RequestParameters<'changeEmail'>}
   * @returns
   */
  updateEmail(
    updateEmailData: operations["changeEmail post /account/change-email"]["body"],
  ): 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<
    Schemas["PaymentMethod"]["translated"] | null
  >;
  /**
   * Default billing address for the user
   */
  userDefaultBillingAddress: ComputedRef<Schemas["CustomerAddress"] | null>;
  /**
   * Default shipping address for the user
   */
  userDefaultShippingAddress: ComputedRef<Schemas["CustomerAddress"] | null>;
};

Properties ​

NameTypeDescription
user
ComputedRef< | 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< | null>
{@link Country} of the user
salutation
Ref< | null>
{@link Salutation} of the user
defaultBillingAddressId
ComputedRef<string | null>
Default billing address id
defaultShippingAddressId
ComputedRef<string | null>
Default shipping address id
userDefaultPaymentMethod
ComputedRef< | null>
Default payment method for the user
userDefaultBillingAddress
ComputedRef< | null>
Default billing address for the user
userDefaultShippingAddress
ComputedRef< | null>
Default shipping address for the user

Methods ​

NameTypeDescription
login
Promise<void>
Logs-in user with given credentials
register
Promise<>
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