mirror of
https://github.com/vercel/commerce.git
synced 2025-05-19 07:56:59 +00:00
implement login
Signed-off-by: Loan Laux <loan@outgrow.io>
This commit is contained in:
parent
49bd38fd82
commit
8f260d66e7
@ -9,16 +9,14 @@ import {
|
|||||||
import { normalizeCart } from '@framework/utils'
|
import { normalizeCart } from '@framework/utils'
|
||||||
|
|
||||||
// Return current cart info
|
// Return current cart info
|
||||||
const getCart: CartHandlers['getCart'] = async ({
|
const getCart: CartHandlers['getCart'] = async ({ req, res, config }) => {
|
||||||
req: {
|
const {
|
||||||
cookies: {
|
cookies: {
|
||||||
[REACTION_ANONYMOUS_CART_TOKEN_COOKIE]: anonymousCartToken,
|
[REACTION_ANONYMOUS_CART_TOKEN_COOKIE]: anonymousCartToken,
|
||||||
[REACTION_CART_ID_COOKIE]: cartId,
|
[REACTION_CART_ID_COOKIE]: cartId,
|
||||||
},
|
},
|
||||||
},
|
} = req
|
||||||
res,
|
|
||||||
config,
|
|
||||||
}) => {
|
|
||||||
let normalizedCart
|
let normalizedCart
|
||||||
|
|
||||||
console.log('get-cart API')
|
console.log('get-cart API')
|
||||||
|
@ -6,7 +6,7 @@ import createApiHandler, {
|
|||||||
import {
|
import {
|
||||||
REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
|
REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
|
||||||
SHOPIFY_CHECKOUT_URL_COOKIE,
|
SHOPIFY_CHECKOUT_URL_COOKIE,
|
||||||
SHOPIFY_CUSTOMER_TOKEN_COOKIE,
|
REACTION_CUSTOMER_TOKEN_COOKIE,
|
||||||
} from '../../const'
|
} from '../../const'
|
||||||
|
|
||||||
import { getConfig } from '..'
|
import { getConfig } from '..'
|
||||||
@ -25,14 +25,14 @@ const checkoutApi: ReactionCommerceApiHandler<any> = async (
|
|||||||
|
|
||||||
const { cookies } = req
|
const { cookies } = req
|
||||||
const checkoutUrl = cookies[SHOPIFY_CHECKOUT_URL_COOKIE]
|
const checkoutUrl = cookies[SHOPIFY_CHECKOUT_URL_COOKIE]
|
||||||
const customerCookie = cookies[SHOPIFY_CUSTOMER_TOKEN_COOKIE]
|
const customerCookie = cookies[REACTION_CUSTOMER_TOKEN_COOKIE]
|
||||||
|
|
||||||
if (customerCookie) {
|
if (customerCookie) {
|
||||||
try {
|
try {
|
||||||
await config.fetch(associateCustomerWithCheckoutMutation, {
|
await config.fetch(associateCustomerWithCheckoutMutation, {
|
||||||
variables: {
|
variables: {
|
||||||
checkoutId: cookies[REACTION_ANONYMOUS_CART_TOKEN_COOKIE],
|
checkoutId: cookies[REACTION_ANONYMOUS_CART_TOKEN_COOKIE],
|
||||||
customerAccessToken: cookies[SHOPIFY_CUSTOMER_TOKEN_COOKIE],
|
customerAccessToken: cookies[REACTION_CUSTOMER_TOKEN_COOKIE],
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -5,8 +5,8 @@ import {
|
|||||||
REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
|
REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
|
||||||
REACTION_CART_ID_COOKIE,
|
REACTION_CART_ID_COOKIE,
|
||||||
REACTION_EMPTY_DUMMY_CART_ID,
|
REACTION_EMPTY_DUMMY_CART_ID,
|
||||||
SHOPIFY_CUSTOMER_TOKEN_COOKIE,
|
REACTION_CUSTOMER_TOKEN_COOKIE,
|
||||||
SHOPIFY_COOKIE_EXPIRE,
|
REACTION_COOKIE_EXPIRE,
|
||||||
SHOP_ID,
|
SHOP_ID,
|
||||||
} from '../const'
|
} from '../const'
|
||||||
|
|
||||||
@ -45,9 +45,9 @@ const config = new Config({
|
|||||||
cartCookie: REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
|
cartCookie: REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
|
||||||
cartIdCookie: REACTION_CART_ID_COOKIE,
|
cartIdCookie: REACTION_CART_ID_COOKIE,
|
||||||
dummyEmptyCartId: REACTION_EMPTY_DUMMY_CART_ID,
|
dummyEmptyCartId: REACTION_EMPTY_DUMMY_CART_ID,
|
||||||
cartCookieMaxAge: SHOPIFY_COOKIE_EXPIRE,
|
cartCookieMaxAge: REACTION_COOKIE_EXPIRE,
|
||||||
fetch: fetchGraphqlApi,
|
fetch: fetchGraphqlApi,
|
||||||
customerCookie: SHOPIFY_CUSTOMER_TOKEN_COOKIE,
|
customerCookie: REACTION_CUSTOMER_TOKEN_COOKIE,
|
||||||
shopId: SHOP_ID,
|
shopId: SHOP_ID,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import type { GraphQLFetcher } from '@commerce/api'
|
import type { GraphQLFetcher } from '@commerce/api'
|
||||||
import fetch from './fetch'
|
import fetch from './fetch'
|
||||||
|
|
||||||
import { API_URL } from '../../const'
|
import { API_URL } from '../../const'
|
||||||
import { getError } from '../../utils/handle-fetch-response'
|
import { getError } from '../../utils/handle-fetch-response'
|
||||||
|
|
||||||
|
3
framework/reactioncommerce/auth/index.ts
Normal file
3
framework/reactioncommerce/auth/index.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export { default as useLogin } from './use-login'
|
||||||
|
export { default as useLogout } from './use-logout'
|
||||||
|
export { default as useSignup } from './use-signup'
|
@ -2,7 +2,7 @@ import { useCallback } from 'react'
|
|||||||
import type { MutationHook } from '@commerce/utils/types'
|
import type { MutationHook } from '@commerce/utils/types'
|
||||||
import { CommerceError, ValidationError } from '@commerce/utils/errors'
|
import { CommerceError, ValidationError } from '@commerce/utils/errors'
|
||||||
import useCustomer from '../customer/use-customer'
|
import useCustomer from '../customer/use-customer'
|
||||||
import createCustomerAccessTokenMutation from '../utils/mutations/customer-access-token-create'
|
import authenticateMutation from '../utils/mutations/authenticate'
|
||||||
import {
|
import {
|
||||||
CustomerAccessTokenCreateInput,
|
CustomerAccessTokenCreateInput,
|
||||||
CustomerUserError,
|
CustomerUserError,
|
||||||
@ -25,7 +25,7 @@ const getErrorMessage = ({ code, message }: CustomerUserError) => {
|
|||||||
|
|
||||||
export const handler: MutationHook<null, {}, CustomerAccessTokenCreateInput> = {
|
export const handler: MutationHook<null, {}, CustomerAccessTokenCreateInput> = {
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
query: createCustomerAccessTokenMutation,
|
query: authenticateMutation,
|
||||||
},
|
},
|
||||||
async fetcher({ input: { email, password }, options, fetch }) {
|
async fetcher({ input: { email, password }, options, fetch }) {
|
||||||
if (!(email && password)) {
|
if (!(email && password)) {
|
||||||
@ -35,25 +35,24 @@ export const handler: MutationHook<null, {}, CustomerAccessTokenCreateInput> = {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const { customerAccessTokenCreate } = await fetch<
|
console.log('querying API')
|
||||||
Mutation,
|
|
||||||
MutationCheckoutCreateArgs
|
const { authenticate } = await fetch<Mutation, MutationCheckoutCreateArgs>({
|
||||||
>({
|
|
||||||
...options,
|
...options,
|
||||||
variables: {
|
variables: {
|
||||||
input: { email, password },
|
serviceName: 'password',
|
||||||
|
params: { user: { email }, password },
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const errors = customerAccessTokenCreate?.customerUserErrors
|
// if (errors && errors.length) {
|
||||||
|
// throw new ValidationError({
|
||||||
|
// message: getErrorMessage(errors[0].message),
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
const accessToken = authenticate?.tokens?.accessToken
|
||||||
|
|
||||||
if (errors && errors.length) {
|
console.log('accessToken', accessToken)
|
||||||
throw new ValidationError({
|
|
||||||
message: getErrorMessage(errors[0]),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
const customerAccessToken = customerAccessTokenCreate?.customerAccessToken
|
|
||||||
const accessToken = customerAccessToken?.accessToken
|
|
||||||
|
|
||||||
if (accessToken) {
|
if (accessToken) {
|
||||||
setCustomerToken(accessToken)
|
setCustomerToken(accessToken)
|
||||||
|
@ -7,7 +7,7 @@ import { CustomerCreateInput } from '../schema'
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
customerCreateMutation,
|
customerCreateMutation,
|
||||||
customerAccessTokenCreateMutation,
|
authenticateMutation,
|
||||||
} from '../utils/mutations'
|
} from '../utils/mutations'
|
||||||
import handleLogin from '../utils/handle-login'
|
import handleLogin from '../utils/handle-login'
|
||||||
|
|
||||||
@ -47,7 +47,7 @@ export const handler: MutationHook<
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const loginData = await fetch({
|
const loginData = await fetch({
|
||||||
query: customerAccessTokenCreateMutation,
|
query: authenticateMutation,
|
||||||
variables: {
|
variables: {
|
||||||
input: {
|
input: {
|
||||||
email,
|
email,
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
|
REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
|
||||||
SHOPIFY_CHECKOUT_URL_COOKIE,
|
SHOPIFY_CHECKOUT_URL_COOKIE,
|
||||||
SHOPIFY_COOKIE_EXPIRE,
|
REACTION_COOKIE_EXPIRE,
|
||||||
} from '../../const'
|
} from '../../const'
|
||||||
|
|
||||||
import checkoutCreateMutation from '../../utils/mutations/checkout-create'
|
import checkoutCreateMutation from '../../utils/mutations/checkout-create'
|
||||||
@ -22,7 +22,7 @@ export const checkoutCreate = async (fetch: any) => {
|
|||||||
|
|
||||||
if (checkoutId) {
|
if (checkoutId) {
|
||||||
const options = {
|
const options = {
|
||||||
expires: SHOPIFY_COOKIE_EXPIRE,
|
expires: REACTION_COOKIE_EXPIRE,
|
||||||
}
|
}
|
||||||
Cookies.set(REACTION_ANONYMOUS_CART_TOKEN_COOKIE, checkoutId, options)
|
Cookies.set(REACTION_ANONYMOUS_CART_TOKEN_COOKIE, checkoutId, options)
|
||||||
Cookies.set(SHOPIFY_CHECKOUT_URL_COOKIE, checkout?.webUrl, options)
|
Cookies.set(SHOPIFY_CHECKOUT_URL_COOKIE, checkout?.webUrl, options)
|
||||||
|
@ -7,12 +7,12 @@ export const REACTION_EMPTY_DUMMY_CART_ID = 'DUMMY_EMPTY_CART_ID'
|
|||||||
|
|
||||||
export const SHOPIFY_CHECKOUT_URL_COOKIE = 'shopify_checkoutUrl'
|
export const SHOPIFY_CHECKOUT_URL_COOKIE = 'shopify_checkoutUrl'
|
||||||
|
|
||||||
export const SHOPIFY_CUSTOMER_TOKEN_COOKIE = 'shopify_customerToken'
|
export const REACTION_CUSTOMER_TOKEN_COOKIE = 'reaction_customerToken'
|
||||||
|
|
||||||
export const STORE_DOMAIN = process.env.NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN
|
export const STORE_DOMAIN = process.env.NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN
|
||||||
|
|
||||||
export const SHOPIFY_COOKIE_EXPIRE = 30
|
export const REACTION_COOKIE_EXPIRE = 30
|
||||||
|
|
||||||
export const API_URL = `http://127.0.0.1:3000/graphql`
|
export const API_URL = `http://127.0.0.1:3000/graphql`
|
||||||
|
|
||||||
export const SHOP_ID = 'cmVhY3Rpb24vc2hvcDplcnBESFlDdzc5cFRBV0FHUg=='
|
export const SHOP_ID = 'cmVhY3Rpb24vc2hvcDpIZGIycnRYTWVpbVRKbzZrcg=='
|
||||||
|
@ -2,6 +2,7 @@ import { FetcherError } from '@commerce/utils/errors'
|
|||||||
import type { Fetcher } from '@commerce/utils/types'
|
import type { Fetcher } from '@commerce/utils/types'
|
||||||
import { handleFetchResponse } from './utils'
|
import { handleFetchResponse } from './utils'
|
||||||
import { API_URL } from './const'
|
import { API_URL } from './const'
|
||||||
|
import { getCustomerToken } from './utils'
|
||||||
|
|
||||||
async function getText(res: Response) {
|
async function getText(res: Response) {
|
||||||
try {
|
try {
|
||||||
@ -28,12 +29,20 @@ const fetcher: Fetcher = async ({
|
|||||||
}) => {
|
}) => {
|
||||||
// if no URL is passed but we have a `query` param, we assume it's GraphQL
|
// if no URL is passed but we have a `query` param, we assume it's GraphQL
|
||||||
if (!url && query) {
|
if (!url && query) {
|
||||||
|
const customerToken = getCustomerToken()
|
||||||
|
const authorizationHeader = {}
|
||||||
|
|
||||||
|
if (customerToken) {
|
||||||
|
authorizationHeader['Authorization'] = `bearer ${customerToken}`
|
||||||
|
}
|
||||||
|
|
||||||
return handleFetchResponse(
|
return handleFetchResponse(
|
||||||
await fetch(API_URL, {
|
await fetch(API_URL, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({ query, variables }),
|
body: JSON.stringify({ query, variables }),
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
...authorizationHeader,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
11351
framework/reactioncommerce/schema.d.ts
vendored
11351
framework/reactioncommerce/schema.d.ts
vendored
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,20 +1,24 @@
|
|||||||
import Cookies, { CookieAttributes } from 'js-cookie'
|
import Cookies, { CookieAttributes } from 'js-cookie'
|
||||||
import { SHOPIFY_COOKIE_EXPIRE, SHOPIFY_CUSTOMER_TOKEN_COOKIE } from '../const'
|
import {
|
||||||
|
REACTION_COOKIE_EXPIRE,
|
||||||
|
REACTION_CUSTOMER_TOKEN_COOKIE,
|
||||||
|
} from '../const'
|
||||||
|
|
||||||
export const getCustomerToken = () => Cookies.get(SHOPIFY_CUSTOMER_TOKEN_COOKIE)
|
export const getCustomerToken = () =>
|
||||||
|
Cookies.get(REACTION_CUSTOMER_TOKEN_COOKIE)
|
||||||
|
|
||||||
export const setCustomerToken = (
|
export const setCustomerToken = (
|
||||||
token: string | null,
|
token: string | null,
|
||||||
options?: CookieAttributes
|
options?: CookieAttributes
|
||||||
) => {
|
) => {
|
||||||
if (!token) {
|
if (!token) {
|
||||||
Cookies.remove(SHOPIFY_CUSTOMER_TOKEN_COOKIE)
|
Cookies.remove(REACTION_CUSTOMER_TOKEN_COOKIE)
|
||||||
} else {
|
} else {
|
||||||
Cookies.set(
|
Cookies.set(
|
||||||
SHOPIFY_CUSTOMER_TOKEN_COOKIE,
|
REACTION_CUSTOMER_TOKEN_COOKIE,
|
||||||
token,
|
token,
|
||||||
options ?? {
|
options ?? {
|
||||||
expires: SHOPIFY_COOKIE_EXPIRE,
|
expires: REACTION_COOKIE_EXPIRE,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
15
framework/reactioncommerce/utils/mutations/authenticate.ts
Normal file
15
framework/reactioncommerce/utils/mutations/authenticate.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
const authenticateMutation = /* GraphQL */ `
|
||||||
|
mutation authenticate(
|
||||||
|
$serviceName: String!
|
||||||
|
$params: AuthenticateParamsInput!
|
||||||
|
) {
|
||||||
|
authenticate(serviceName: $serviceName, params: $params) {
|
||||||
|
sessionId
|
||||||
|
tokens {
|
||||||
|
refreshToken
|
||||||
|
accessToken
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
export default authenticateMutation
|
@ -1,16 +0,0 @@
|
|||||||
const customerAccessTokenCreateMutation = /* GraphQL */ `
|
|
||||||
mutation customerAccessTokenCreate($input: CustomerAccessTokenCreateInput!) {
|
|
||||||
customerAccessTokenCreate(input: $input) {
|
|
||||||
customerAccessToken {
|
|
||||||
accessToken
|
|
||||||
expiresAt
|
|
||||||
}
|
|
||||||
customerUserErrors {
|
|
||||||
code
|
|
||||||
field
|
|
||||||
message
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`
|
|
||||||
export default customerAccessTokenCreateMutation
|
|
@ -1,7 +1,7 @@
|
|||||||
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 customerCreateMutation } from './customer-create'
|
||||||
export { default as checkoutCreateMutation } from './checkout-create'
|
export { default as checkoutCreateMutation } from './checkout-create'
|
||||||
export { default as customerAccessTokenCreateMutation } from './customer-access-token-create'
|
export { default as authenticateMutation } from './authenticate'
|
||||||
export { default as customerAccessTokenDeleteMutation } from './customer-access-token-delete'
|
export { default as customerAccessTokenDeleteMutation } from './customer-access-token-delete'
|
||||||
export { default as removeCartItemsMutation } from './remove-cart-items'
|
export { default as removeCartItemsMutation } from './remove-cart-items'
|
||||||
export { default as updateCartItemsQuantityMutation } from './update-cart-items-quantity'
|
export { default as updateCartItemsQuantityMutation } from './update-cart-items-quantity'
|
||||||
|
@ -6,6 +6,9 @@ const isShopify = commerce.provider === 'shopify'
|
|||||||
const isRC = commerce.provider === 'reactioncommerce'
|
const isRC = commerce.provider === 'reactioncommerce'
|
||||||
|
|
||||||
module.exports = withCommerceConfig({
|
module.exports = withCommerceConfig({
|
||||||
|
env: {
|
||||||
|
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
|
||||||
|
},
|
||||||
commerce,
|
commerce,
|
||||||
i18n: {
|
i18n: {
|
||||||
locales: ['en-US', 'es'],
|
locales: ['en-US', 'es'],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user