getMe
Fetch current user data (i.e cart and customer profile).
In turn, it fetches a customer from commercetools by sending a request to its GraphQL API.
If the customer parameter is not passed, this method uses the getMeBasicProfileDefaultQuery GraphQL query. Else, it uses the getMeFullProfileDefaultQuery query.
When vsf-commercetools-token cookie is present in the browser, this method will fetch data of the user (anonymous or authenticated) linked to the token. In case there is no cookie, it will return an empty data object.
Signature
declare function getMe(
context: CommercetoolsIntegrationContext,
params?: GetMeParams,
customQuery?: CustomQuery
): Promise<GetMeResponse>;Parameters
| Name | Required | Type | Description |
|---|---|---|---|
context | Required | CommercetoolsIntegrationContext | |
params | Optional | GetMeParams | |
customQuery | Optional | CustomQuery |
Returns
Returns a representation of the GetMeResponse.
Referenced Types
Examples
Fetching basic user profile (i.e. cart).
const { me } = await sdk.commerce.getMe();Fetching full user profile (i.e. cart and customer profile).
const { me } = await sdk.commerce.getMe({ customer: true });Creating a custom GraphQL query for fetching basic user profile in middleware.config.ts.
export const integrations = {
ct: {
// ...
customQueries: {
'get-me-basic-custom-query': ({ query, variables, metadata }) => ({
variables,
query: `
query getBasicProfile {
me {
activeCart {
${metadata}
}
}
}
`
}),
}
}
}
};Fetching basic user profile and using the customQuery parameter to leverage the custom GraphQL query from the previous example.
const { me } = await sdk.commerce.getMe(undefined, {
getBasicProfile: 'get-me-basic-custom-query',
metadata: 'id,version'
});Creating a custom GraphQL query for fetching full user profile in middleware.config.ts.
export const integrations = {
ct: {
// ...
customQueries: {
'get-me-full-custom-query': ({ query, variables, metadata }) => ({
variables,
query: `
query getFullProfile {
me {
activeCart {
${metadata.activeCart}
}
customer {
${metadata.customer}
}
}
}
`
}),
}
}
}
};Fetching full user profile and using the customQuery parameter to leverage the custom GraphQL query from the previous example.
const { me } = await sdk.commerce.getMe({ customer: true }, {
getFullProfile: 'get-me-full-custom-query',
metadata: { activeCart: 'id', customer: 'id' }
});