diff --git a/framework/shopify/auth/use-login.tsx b/framework/shopify/auth/use-login.tsx index 516d0b14a..4811bbe8d 100644 --- a/framework/shopify/auth/use-login.tsx +++ b/framework/shopify/auth/use-login.tsx @@ -5,27 +5,12 @@ import useCommerceLogin from '@commerce/use-login' import useCustomer from '../customer/use-customer' import createCustomerAccessTokenMutation from '../utils/mutations/customer-access-token-create' import { CustomerAccessTokenCreateInput } from '@framework/schema' -import { setCustomerToken } from '@framework/utils/customer-token' +import handleLogin from '@framework/utils/handle-login' const defaultOpts = { query: createCustomerAccessTokenMutation, } -const getErrorMessage = ({ - code, - message, -}: { - code: string - message: string -}) => { - switch (code) { - case 'UNIDENTIFIED_CUSTOMER': - message = 'Cannot find an account that matches the provided credentials' - break - } - return message -} - export const fetcher: HookFetcher = ( options, input, @@ -42,25 +27,7 @@ export const fetcher: HookFetcher = ( ...defaultOpts, ...options, variables: { input }, - }).then((data) => { - const response = data?.customerAccessTokenCreate - const errors = response?.customerUserErrors - - if (errors && errors.length) { - throw new ValidationError({ - message: getErrorMessage(errors[0]), - }) - } - - const customerAccessToken = response?.customerAccessToken - const accessToken = customerAccessToken?.accessToken - - if (accessToken) { - setCustomerToken(accessToken) - } - - return customerAccessToken - }) + }).then(handleLogin) } export function extendHook(customFetcher: typeof fetcher) { diff --git a/framework/shopify/auth/use-signup.tsx b/framework/shopify/auth/use-signup.tsx index 7184554bb..c1b40e60c 100644 --- a/framework/shopify/auth/use-signup.tsx +++ b/framework/shopify/auth/use-signup.tsx @@ -3,9 +3,14 @@ import type { HookFetcher } from '@commerce/utils/types' import { CommerceError } from '@commerce/utils/errors' import useCommerceSignup from '@commerce/use-signup' import useCustomer from '../customer/use-customer' -import customerCreateMutation from '@framework/utils/mutations/customer-create' import { CustomerCreateInput } from '@framework/schema' +import { + customerCreateMutation, + customerAccessTokenCreateMutation, +} from '@framework/utils/mutations' +import handleLogin from '@framework/utils/handle-login' + const defaultOpts = { query: customerCreateMutation, } @@ -21,12 +26,23 @@ export const fetcher: HookFetcher = ( 'A first name, last name, email and password are required to signup', }) } - return fetch({ ...defaultOpts, ...options, variables: { input }, - }).then((data) => { + }).then(async (data) => { + try { + const loginData = await fetch({ + query: customerAccessTokenCreateMutation, + variables: { + input: { + email: input.email, + password: input.password, + }, + }, + }) + handleLogin(loginData) + } catch (error) {} return data }) } diff --git a/framework/shopify/utils/handle-login.ts b/framework/shopify/utils/handle-login.ts new file mode 100644 index 000000000..81a016240 --- /dev/null +++ b/framework/shopify/utils/handle-login.ts @@ -0,0 +1,39 @@ +import { ValidationError } from '@commerce/utils/errors' +import { setCustomerToken } from './customer-token' + +const getErrorMessage = ({ + code, + message, +}: { + code: string + message: string +}) => { + switch (code) { + case 'UNIDENTIFIED_CUSTOMER': + message = 'Cannot find an account that matches the provided credentials' + break + } + return message +} + +const handleLogin = (data: any) => { + const response = data?.customerAccessTokenCreate + const errors = response?.customerUserErrors + + if (errors && errors.length) { + throw new ValidationError({ + message: errors[0], + }) + } + + const customerAccessToken = response?.customerAccessToken + const accessToken = customerAccessToken?.accessToken + + if (accessToken) { + setCustomerToken(accessToken) + } + + return customerAccessToken +} + +export default handleLogin diff --git a/framework/shopify/utils/mutations/index.ts b/framework/shopify/utils/mutations/index.ts index fe1beb82d..5ccf5b1dd 100644 --- a/framework/shopify/utils/mutations/index.ts +++ b/framework/shopify/utils/mutations/index.ts @@ -3,5 +3,6 @@ export { default as checkoutCreateMutation } from './checkout-create' export { default as checkoutLineItemAddMutation } from './checkout-line-item-add' export { default as checkoutLineItemUpdateMutation } from './checkout-create' export { default as checkoutLineItemRemoveMutation } from './checkout-line-item-remove' +export { default as customerCreateMutation } from './customer-create' export { default as customerAccessTokenCreateMutation } from './customer-access-token-create' export { default as customerAccessTokenDeleteMutation } from './customer-access-token-delete'