Session
The Session module manages the state of session-related information in the customer's browser, including currency, channel, localization, and customer data. It ensures session validation and synchronization with the shopping cart, providing a consistent and up-to-date shopping experience.
Session object structure
You can see details of the session data managed by the module below.
Session
Field | Type | Description |
---|---|---|
addressType | string | Specifies the type of address associated with the session. Possible values are: shipping (delivery addresses), billing (payment addresses), or null if not specified. |
b2b | object | Contains B2B information. See [B2B] for more details. |
channel | string | Indicates the sales channel associated with the session. |
country | string | Indicates the country associated with the session in a three-letter ISO code (example: USA ). |
currency | object | Contains currency details for the session. See Currency for more information. |
geoCoordinates | object | Contains geographical coordinates for the user session. See GeoCoordinates for details. |
locale | string | Indicates the locale of the session, including language and region (example: "en-US"). |
person | object | Contains information about the customer. See [Person] for details. |
postalCode | string | Indicates the postal code associated with the session. |
b2b
Field | Type | Description |
---|---|---|
customerID | String | Identifies the buyer organization the customer belongs to. |
geoCoordinates
Field | Type | Description |
---|---|---|
latitude | number | Indicates the latitude coordinate of the geographical location. |
symbol | number | Indicates the longitude coordinate of the geographical location. |
currency
Field | Type | Description |
---|---|---|
code | String | Indicates the currency code in three-letter ISO format (example: USD ). |
symbol | String | Indicates the symbol associated with the currency (example: $ ). |
person
Field | Type | Description |
---|---|---|
id | String | Provides the customer identifier, if available in the ecommerce platform. |
email | String | Stores the email address provided by the customer. |
givenName | String | Contains the customer’s first name. |
familyName | String | Contains the customer’s last name. |
Import
The Session module exports a
createSessionStore
function and Session
type, which you can import like this:
_10import { createSessionStore } from '@faststore/sdk'_10import type { Session } from '@faststore/sdk'
Usage
The following example demonstrates how to manage a session using
createSessionStore
.- The
createSessionStore
function initializes a session store with the provided default session and an optional validation function. - The
validateSession
function sends the current session data to the FastStore API for validation and returns the validated session.
_72_72import { gql } from '@faststore/graphql-utils'_72import { createSessionStore } from '@faststore/sdk'_72import { useMemo } from 'react'_72import type { Session } from '@faststore/sdk'_72_72import storeConfig from 'store.config'_72_72import { cartStore } from '../cart'_72import { request } from '../graphql/request'_72import { createValidationStore, useStore } from '../useStore'_72import type {_72 ValidateSessionMutation,_72 ValidateSessionMutationVariables,_72} from '../../../@generated/graphql/index'_72_72export const mutation = gql`_72 mutation ValidateSession($session: IStoreSession!, $search: String!) {_72 validateSession(session: $session, search: $search) {_72 locale_72 channel_72 country_72 postalCode_72 currency {_72 code_72 symbol_72 }_72 person {_72 id_72 email_72 givenName_72 familyName_72 }_72 }_72 }_72`_72_72export const validateSession = async (session: Session) => {_72 const data = await request<_72 ValidateSessionMutation,_72 ValidateSessionMutationVariables_72 >(mutation, { session, search: window.location.search })_72_72 return data.validateSession_72}_72_72const [validationStore, onValidate] = createValidationStore(validateSession)_72_72const defaultStore = createSessionStore(storeConfig.session, onValidate)_72_72export const sessionStore = {_72 ...defaultStore,_72 set: (val: Session) => {_72 defaultStore.set(val)_72_72 // Trigger cart revalidation when session changes_72 cartStore.set(cartStore.read())_72 },_72}_72_72export const useSession = () => {_72 const session = useStore(sessionStore)_72 const isValidating = useStore(validationStore)_72_72 return useMemo(_72 () => ({_72 ...session,_72 isValidating,_72 }),_72 [isValidating, session]_72 )_72}