mirror of
https://github.com/vercel/commerce.git
synced 2025-03-14 22:42:33 +00:00
Automatic login after sign-up
This commit is contained in:
parent
dde09c5105
commit
41b14e55c3
@ -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<null, CustomerAccessTokenCreateInput> = (
|
||||
options,
|
||||
input,
|
||||
@ -42,25 +27,7 @@ export const fetcher: HookFetcher<null, CustomerAccessTokenCreateInput> = (
|
||||
...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) {
|
||||
|
@ -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<null, CustomerCreateInput> = (
|
||||
'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
|
||||
})
|
||||
}
|
||||
|
39
framework/shopify/utils/handle-login.ts
Normal file
39
framework/shopify/utils/handle-login.ts
Normal file
@ -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
|
@ -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'
|
||||
|
Loading…
x
Reference in New Issue
Block a user