implement sign-up

Signed-off-by: Loan Laux <loan@outgrow.io>
This commit is contained in:
Loan Laux 2021-04-27 17:23:44 +04:00
parent 25ba1f1bae
commit 3563e93a3b
No known key found for this signature in database
GPG Key ID: AF9E9BD6548AD52E
7 changed files with 34 additions and 58 deletions

View File

@ -7,7 +7,7 @@ import {
CustomerAccessTokenCreateInput, CustomerAccessTokenCreateInput,
CustomerUserError, CustomerUserError,
Mutation, Mutation,
MutationCheckoutCreateArgs, MutationAuthenticateArgs,
} from '../schema' } from '../schema'
import useLogin, { UseLogin } from '@commerce/auth/use-login' import useLogin, { UseLogin } from '@commerce/auth/use-login'
import { setCustomerToken } from '../utils' import { setCustomerToken } from '../utils'
@ -37,7 +37,7 @@ export const handler: MutationHook<null, {}, CustomerAccessTokenCreateInput> = {
console.log('querying API') console.log('querying API')
const { authenticate } = await fetch<Mutation, MutationCheckoutCreateArgs>({ const { authenticate } = await fetch<Mutation, MutationAuthenticateArgs>({
...options, ...options,
variables: { variables: {
serviceName: 'password', serviceName: 'password',
@ -45,11 +45,6 @@ export const handler: MutationHook<null, {}, CustomerAccessTokenCreateInput> = {
}, },
}) })
// if (errors && errors.length) {
// throw new ValidationError({
// message: getErrorMessage(errors[0].message),
// })
// }
const accessToken = authenticate?.tokens?.accessToken const accessToken = authenticate?.tokens?.accessToken
console.log('accessToken', accessToken) console.log('accessToken', accessToken)

View File

@ -2,62 +2,45 @@ import { useCallback } from 'react'
import type { MutationHook } from '@commerce/utils/types' import type { MutationHook } from '@commerce/utils/types'
import { CommerceError } from '@commerce/utils/errors' import { CommerceError } from '@commerce/utils/errors'
import useSignup, { UseSignup } from '@commerce/auth/use-signup' import useSignup, { UseSignup } from '@commerce/auth/use-signup'
import { setCustomerToken } from '@framework/utils'
import { createUserMutation } from '@framework/utils/mutations'
import useCustomer from '../customer/use-customer' import useCustomer from '../customer/use-customer'
import { CustomerCreateInput } from '../schema' import { CreateUserInput } from '../schema'
import {
customerCreateMutation,
authenticateMutation,
} from '../utils/mutations'
import handleLogin from '../utils/handle-login'
export default useSignup as UseSignup<typeof handler> export default useSignup as UseSignup<typeof handler>
export const handler: MutationHook< export const handler: MutationHook<
null, null,
{}, {},
CustomerCreateInput, CreateUserInput,
CustomerCreateInput CreateUserInput
> = { > = {
fetchOptions: { fetchOptions: {
query: customerCreateMutation, query: createUserMutation,
}, },
async fetcher({ async fetcher({ input: { email, password }, options, fetch }) {
input: { firstName, lastName, email, password }, if (!(email && password)) {
options,
fetch,
}) {
if (!(firstName && lastName && email && password)) {
throw new CommerceError({ throw new CommerceError({
message: message: 'An email and password are required to sign up',
'A first name, last name, email and password are required to signup',
}) })
} }
const data = await fetch({ const { createUser } = await fetch({
...options, ...options,
variables: { variables: {
input: { input: {
firstName,
lastName,
email, email,
password, password,
}, },
}, },
}) })
try { const accessToken = createUser?.loginResult?.tokens?.accessToken
const loginData = await fetch({
query: authenticateMutation, if (accessToken) {
variables: { setCustomerToken(accessToken)
input: { }
email,
password, return createUser
},
},
})
handleLogin(loginData)
} catch (error) {}
return data
}, },
useHook: ({ fetch }) => () => { useHook: ({ fetch }) => () => {
const { revalidate } = useCustomer() const { revalidate } = useCustomer()

View File

@ -1,5 +1,5 @@
import { getConfig, ReactionCommerceConfig } from '../api' import { getConfig, ReactionCommerceConfig } from '../api'
import getViewerIdQuery from '../utils/queries/get-customer-id-query' import getViewerIdQuery from '../utils/queries/get-viewer-id-query'
async function getViewerId({ async function getViewerId({
customerToken: customerAccessToken, customerToken: customerAccessToken,

View File

@ -0,0 +1,13 @@
const createUserMutation = /* GraphQL */ `
mutation createUser($input: CreateUserInput!) {
createUser(user: $input) {
loginResult {
tokens {
refreshToken
accessToken
}
}
}
}
`
export default createUserMutation

View File

@ -1,15 +0,0 @@
const customerCreateMutation = /* GraphQL */ `
mutation customerCreate($input: CustomerCreateInput!) {
customerCreate(input: $input) {
customerUserErrors {
code
field
message
}
customer {
id
}
}
}
`
export default customerCreateMutation

View File

@ -1,5 +1,5 @@
export { default as addCartItemsMutation } from './add-cart-items' export { default as addCartItemsMutation } from './add-cart-items'
export { default as customerCreateMutation } from './customer-create' export { default as createUserMutation } from './create-user'
export { default as checkoutCreateMutation } from './checkout-create' export { default as checkoutCreateMutation } from './checkout-create'
export { default as authenticateMutation } from './authenticate' export { default as authenticateMutation } from './authenticate'
export { default as logoutMutation } from './logout' export { default as logoutMutation } from './logout'