diff --git a/framework/saleor/auth/use-login.tsx b/framework/saleor/auth/use-login.tsx index a8c240106..2c784ec91 100644 --- a/framework/saleor/auth/use-login.tsx +++ b/framework/saleor/auth/use-login.tsx @@ -9,7 +9,7 @@ import { MutationTokenCreateArgs, } from '../schema' import useLogin, { UseLogin } from '@commerce/auth/use-login' -import { setCSRFToken, setToken, throwUserErrors } from '../utils' +import { setCSRFToken, setToken, throwUserErrors, checkoutAttach, getCheckoutId } from '../utils' export default useLogin as UseLogin @@ -40,6 +40,14 @@ export const handler: MutationHook = { if (token && csrfToken) { setToken(token) setCSRFToken(csrfToken) + + const { checkoutId } = getCheckoutId(); + checkoutAttach(fetch, { + variables: { checkoutId }, + headers: { + Authorization: `JWT ${token}` + } + }) } return null diff --git a/framework/saleor/utils/checkout-attach.ts b/framework/saleor/utils/checkout-attach.ts new file mode 100644 index 000000000..e032e33b2 --- /dev/null +++ b/framework/saleor/utils/checkout-attach.ts @@ -0,0 +1,16 @@ +import * as mutation from './mutations' +import { CheckoutCustomerAttach } from '../schema' + +export const checkoutAttach = async ( + fetch: any, + { variables, headers }: any, +): Promise => { + const data = await fetch({ + query: mutation.AttachCheckout, + variables, + headers + }) + + return data; +} + diff --git a/framework/saleor/utils/index.ts b/framework/saleor/utils/index.ts index 9fd590d38..b84d3ed8a 100644 --- a/framework/saleor/utils/index.ts +++ b/framework/saleor/utils/index.ts @@ -5,10 +5,14 @@ export { default as getSearchVariables } from './get-search-variables' export { default as getVendors } from './get-vendors' export { default as getCategories } from './get-categories' export { default as getCheckoutId } from './get-checkout-id' + export { default as checkoutCreate } from './checkout-create' +export { checkoutAttach } from './checkout-attach'; + export { default as checkoutToCart } from './checkout-to-cart' export { default as handleLogin, handleAutomaticLogin } from './handle-login' export { default as throwUserErrors } from './throw-user-errors' + export * from './queries' export * from './mutations' export * from './normalize' diff --git a/framework/saleor/utils/mutations/associate-customer-with-checkout.ts b/framework/saleor/utils/mutations/attach-checkout.ts similarity index 55% rename from framework/saleor/utils/mutations/associate-customer-with-checkout.ts rename to framework/saleor/utils/mutations/attach-checkout.ts index 66346a4d7..31247c521 100644 --- a/framework/saleor/utils/mutations/associate-customer-with-checkout.ts +++ b/framework/saleor/utils/mutations/attach-checkout.ts @@ -1,5 +1,5 @@ -export const checkoutCustomerAttach = /* GraphQl */ ` - mutation associateCustomerWithCheckout($checkoutId: ID!) { +export const AttachCheckout = /* GraphQl */ ` + mutation AttachCheckout($checkoutId: ID!) { checkoutCustomerAttach(checkoutId: $checkoutId) { errors { message diff --git a/framework/saleor/utils/mutations/index.ts b/framework/saleor/utils/mutations/index.ts index 7ae8dbd5e..dd25b0e5c 100644 --- a/framework/saleor/utils/mutations/index.ts +++ b/framework/saleor/utils/mutations/index.ts @@ -5,3 +5,4 @@ export { checkoutLineUpdate } from './checkout-line-item-update' export { checkoutLineDelete } from './checkout-line-item-remove' export { sessionCreate } from './customer-access-token-create' export { sessionDestroy } from './customer-access-token-delete' +export { AttachCheckout } from './attach-checkout'