Documentation
Feedback
Guides
Storefront Development

Storefront Development
FastStore
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

FieldTypeDescription
addressTypestringSpecifies the type of address associated with the session. Possible values are: shipping (delivery addresses), billing (payment addresses), or null if not specified.
b2bobjectContains B2B information. See [B2B] for more details.
channelstringIndicates the sales channel associated with the session.
countrystringIndicates the country associated with the session in a three-letter ISO code (example: USA).
currencyobjectContains currency details for the session. See Currency for more information.
geoCoordinatesobjectContains geographical coordinates for the user session. See GeoCoordinates for details.
localestringIndicates the locale of the session, including language and region (example: "en-US").
personobjectContains information about the customer. See [Person] for details.
postalCodestringIndicates the postal code associated with the session.

b2b

FieldTypeDescription
customerIDStringIdentifies the buyer organization the customer belongs to.

geoCoordinates

FieldTypeDescription
latitudenumberIndicates the latitude coordinate of the geographical location.
symbolnumberIndicates the longitude coordinate of the geographical location.

currency

FieldTypeDescription
codeStringIndicates the currency code in three-letter ISO format (example: USD).
symbolStringIndicates the symbol associated with the currency (example: $).

person

FieldTypeDescription
idStringProvides the customer identifier, if available in the ecommerce platform.
emailStringStores the email address provided by the customer.
givenNameStringContains the customer’s first name.
familyNameStringContains the customer’s last name.

Import

The Session module exports a createSessionStore function and Session type, which you can import like this:

_10
import { createSessionStore } from '@faststore/sdk'
_10
import 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
_72
import { gql } from '@faststore/graphql-utils'
_72
import { createSessionStore } from '@faststore/sdk'
_72
import { useMemo } from 'react'
_72
import type { Session } from '@faststore/sdk'
_72
_72
import storeConfig from 'store.config'
_72
_72
import { cartStore } from '../cart'
_72
import { request } from '../graphql/request'
_72
import { createValidationStore, useStore } from '../useStore'
_72
import type {
_72
ValidateSessionMutation,
_72
ValidateSessionMutationVariables,
_72
} from '../../../@generated/graphql/index'
_72
_72
export 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
_72
export 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
_72
const [validationStore, onValidate] = createValidationStore(validateSession)
_72
_72
const defaultStore = createSessionStore(storeConfig.session, onValidate)
_72
_72
export 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
_72
export 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
}

Contributors
1
Photo of the contributor
Was this helpful?
Yes
No
Suggest Edits (GitHub)
Contributors
1
Photo of the contributor
On this page