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,
CustomerUserError,
Mutation,
MutationCheckoutCreateArgs,
MutationAuthenticateArgs,
} from '../schema'
import useLogin, { UseLogin } from '@commerce/auth/use-login'
import { setCustomerToken } from '../utils'
@ -37,7 +37,7 @@ export const handler: MutationHook<null, {}, CustomerAccessTokenCreateInput> = {
console.log('querying API')
const { authenticate } = await fetch<Mutation, MutationCheckoutCreateArgs>({
const { authenticate } = await fetch<Mutation, MutationAuthenticateArgs>({
...options,
variables: {
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
console.log('accessToken', accessToken)

View File

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

View File

@ -1,5 +1,5 @@
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({
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 customerCreateMutation } from './customer-create'
export { default as createUserMutation } from './create-user'
export { default as checkoutCreateMutation } from './checkout-create'
export { default as authenticateMutation } from './authenticate'
export { default as logoutMutation } from './logout'