Skip to content

Commit

Permalink
customer account client stub
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinfirman committed Mar 21, 2024
1 parent 51e40a6 commit 7d5477f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/hydrogen/src/customer/constants.ts
Expand Up @@ -4,3 +4,11 @@ export const DEFAULT_CUSTOMER_API_VERSION = '2024-01';
export const USER_AGENT = `Shopify Hydrogen ${LIB_VERSION}`;
export const CUSTOMER_API_CLIENT_ID = '30243aa5-17c1-465a-8493-944bcc4e88aa';
export const CUSTOMER_ACCOUNT_SESSION_KEY = 'customerAccount';
export const CUSTOMER_ACCOUNT_STOREFRONT_SESSION_KEY = 'storefrontCustomerAccountToken';
export const CUSTOMER_ACCOUNT_TOKEN_CREATE = `#graphql
mutation storefrontCustomerAccessTokenCreate {
storefrontCustomerAccessTokenCreate {
customerAccessToken
}
}
`;
12 changes: 12 additions & 0 deletions packages/hydrogen/src/customer/customer.ts
Expand Up @@ -3,6 +3,8 @@ import type {WritableDeep} from 'type-fest';
import {
DEFAULT_CUSTOMER_API_VERSION,
CUSTOMER_ACCOUNT_SESSION_KEY,
CUSTOMER_ACCOUNT_STOREFRONT_SESSION_KEY,
CUSTOMER_ACCOUNT_TOKEN_CREATE,
USER_AGENT,
} from './constants';
import {
Expand Down Expand Up @@ -243,6 +245,15 @@ export function createCustomerAccountClient({
return session.get(CUSTOMER_ACCOUNT_SESSION_KEY)?.accessToken;
}

async function getStorefrontAccessToken() {
const storefrontAccessToken = session.get(CUSTOMER_ACCOUNT_STOREFRONT_SESSION_KEY);

// Check if token is not null and not expired
// Request a new token if needed

return storefrontAccessToken;
}

return {
login: async (options?: LoginOptions) => {
ifInvalidCredentialThrowError(customerAccountUrl, customerAccountId);
Expand Down Expand Up @@ -314,6 +325,7 @@ export function createCustomerAccountClient({
isLoggedIn,
handleAuthStatus,
getAccessToken,
getStorefrontAccessToken,
getApiUrl: () => customerAccountApiUrl,
mutate(mutation, options?) {
ifInvalidCredentialThrowError(customerAccountUrl, customerAccountId);
Expand Down
2 changes: 2 additions & 0 deletions packages/hydrogen/src/customer/types.ts
Expand Up @@ -69,6 +69,8 @@ export type CustomerAccount = {
handleAuthStatus: () => void | DataFunctionValue;
/** Returns CustomerAccessToken if the customer is logged in. It also run a expiry check and does a token refresh if needed. */
getAccessToken: () => Promise<string | undefined>;
/** Returns a store front access token from the `storefrontCustomerAccessTokenCreate` mutation */
getStorefrontAccessToken: () => Promise<string | undefined>;
/** Creates the fully-qualified URL to your store's GraphQL endpoint.*/
getApiUrl: () => string;
/** Logout the customer by clearing the session and redirecting to the login domain. It should be called and returned from a Remix action. The path app should redirect to after logout can be setup in Customer Account API settings in admin.*/
Expand Down

0 comments on commit 7d5477f

Please sign in to comment.