Merge branch 'master' of github.com:vercel/commerce into outgrow-reaction-commerce-provider

Signed-off-by: Loan Laux <loan@outgrow.io>
This commit is contained in:
Loan Laux
2021-06-14 11:31:52 +04:00
190 changed files with 34523 additions and 1653 deletions

View File

@@ -0,0 +1,63 @@
import { useCallback } from 'react'
import type { MutationHook } from '@commerce/utils/types'
import { CommerceError } from '@commerce/utils/errors'
import useCustomer from '../customer/use-customer'
import * as mutation from '../utils/mutations'
import { Mutation, MutationTokenCreateArgs } from '../schema'
import useLogin, { UseLogin } from '@commerce/auth/use-login'
import { setCSRFToken, setToken, throwUserErrors, checkoutAttach, getCheckoutId } from '../utils'
import { LoginHook } from '@commerce/types/login'
export default useLogin as UseLogin<typeof handler>
export const handler: MutationHook<LoginHook> = {
fetchOptions: {
query: mutation.SessionCreate,
},
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 login',
})
}
const { tokenCreate } = await fetch<Mutation, MutationTokenCreateArgs>({
...options,
variables: { email, password },
})
throwUserErrors(tokenCreate?.errors)
const { token, csrfToken } = tokenCreate!
if (token && csrfToken) {
setToken(token)
setCSRFToken(csrfToken)
const { checkoutId } = getCheckoutId()
checkoutAttach(fetch, {
variables: { checkoutId },
headers: {
Authorization: `JWT ${token}`,
},
})
}
return null
},
useHook:
({ fetch }) =>
() => {
const { revalidate } = useCustomer()
return useCallback(
async function login(input) {
const data = await fetch({ input })
await revalidate()
return data
},
[fetch, revalidate]
)
},
}

View File

@@ -0,0 +1,41 @@
import { useCallback } from 'react'
import type { MutationHook } from '@commerce/utils/types'
import useLogout, { UseLogout } from '@commerce/auth/use-logout'
import useCustomer from '../customer/use-customer'
import * as mutation from '../utils/mutations'
import { setCSRFToken, setToken, setCheckoutToken } from '../utils/customer-token'
import { LogoutHook } from '@commerce/types/logout'
export default useLogout as UseLogout<typeof handler>
export const handler: MutationHook<LogoutHook> = {
fetchOptions: {
query: mutation.SessionDestroy,
},
async fetcher({ options, fetch }) {
await fetch({
...options,
variables: {},
})
setToken()
setCSRFToken()
setCheckoutToken()
return null
},
useHook:
({ fetch }) =>
() => {
const { mutate } = useCustomer()
return useCallback(
async function logout() {
const data = await fetch()
await mutate(null, false)
return data
},
[fetch, mutate]
)
},
}

View File

@@ -0,0 +1,56 @@
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 useCustomer from '../customer/use-customer'
import { AccountRegisterInput, Mutation, MutationAccountRegisterArgs } from '../schema'
import * as mutation from '../utils/mutations'
import { handleAutomaticLogin, throwUserErrors } from '../utils'
import { SignupHook } from '@commerce/types/signup'
export default useSignup as UseSignup<typeof handler>
export const handler: MutationHook<SignupHook> = {
fetchOptions: {
query: mutation.AccountCreate,
},
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',
})
}
const { customerCreate } = await fetch<Mutation, MutationAccountRegisterArgs>({
...options,
variables: {
input: {
email,
password,
redirectUrl: 'https://localhost.com',
channel: 'default-channel',
},
},
})
throwUserErrors(customerCreate?.errors)
await handleAutomaticLogin(fetch, { email, password })
return null
},
useHook:
({ fetch }) =>
() => {
const { revalidate } = useCustomer()
return useCallback(
async function signup(input) {
const data = await fetch({ input })
await revalidate()
return data
},
[fetch, revalidate]
)
},
}