From 166bb037e466bf88820df3ca12e2ec6b490366c7 Mon Sep 17 00:00:00 2001 From: George FitzGibbons Date: Thu, 1 Jul 2021 12:21:01 -0400 Subject: [PATCH] Handle checkout for logged in users (#405) * create a jwt token if there is a customerId, move the get customer id to the main utils folder. Need to add in more value to the env file. Updated the env sample. * remove yarn.lock and tsconfig.json * remove build settings * remove build settings * remove build settings * Update tsconfig.json * Delete package-lock.json * fix typescript errors * Update tsconfig.json Co-authored-by: George Fitzgibbons --- .env.template | 4 ++ .../api/endpoints/checkout/checkout.ts | 38 ++++++++++++++++--- .../api/endpoints/wishlist/add-item.ts | 2 +- .../api/endpoints/wishlist/get-wishlist.ts | 2 +- .../api/endpoints/wishlist/remove-item.ts | 2 +- framework/bigcommerce/api/index.ts | 9 +++++ .../wishlist => }/utils/get-customer-id.ts | 4 +- next-env.d.ts | 1 + package.json | 3 +- 9 files changed, 54 insertions(+), 11 deletions(-) rename framework/bigcommerce/api/{endpoints/wishlist => }/utils/get-customer-id.ts (83%) diff --git a/.env.template b/.env.template index c5543e4b1..23627e5fb 100644 --- a/.env.template +++ b/.env.template @@ -7,6 +7,10 @@ BIGCOMMERCE_STORE_API_URL= BIGCOMMERCE_STORE_API_TOKEN= BIGCOMMERCE_STORE_API_CLIENT_ID= BIGCOMMERCE_CHANNEL_ID= +BIGCOMMERCE_STORE_URL= +BIGCOMMERCE_STORE_API_STORE_HASH= +BIGCOMMERCE_STORE_API_CLIENT_SECRET= + NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN= NEXT_PUBLIC_SHOPIFY_STOREFRONT_ACCESS_TOKEN= diff --git a/framework/bigcommerce/api/endpoints/checkout/checkout.ts b/framework/bigcommerce/api/endpoints/checkout/checkout.ts index 517a57950..15c834557 100644 --- a/framework/bigcommerce/api/endpoints/checkout/checkout.ts +++ b/framework/bigcommerce/api/endpoints/checkout/checkout.ts @@ -1,4 +1,7 @@ import type { CheckoutEndpoint } from '.' +import getCustomerId from '../../utils/get-customer-id' +import jwt from 'jsonwebtoken' +import { uuid } from 'uuidv4' const fullCheckout = true @@ -9,22 +12,47 @@ const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({ }) => { const { cookies } = req const cartId = cookies[config.cartCookie] - + const customerToken = cookies[config.customerCookie] if (!cartId) { res.redirect('/cart') return } - const { data } = await config.storeApiFetch( `/v3/carts/${cartId}/redirect_urls`, { method: 'POST', } ) + const customerId = + customerToken && (await getCustomerId({ customerToken, config })) - if (fullCheckout) { - res.redirect(data.checkout_url) - return + //if there is a customer create a jwt token + if (!customerId) { + if (fullCheckout) { + res.redirect(data.checkout_url) + return + } + } else { + const dateCreated = Math.round(new Date().getTime() / 1000) + const payload = { + iss: config.storeApiClientId, + iat: dateCreated, + jti: uuid(), + operation: 'customer_login', + store_hash: config.storeHash, + customer_id: customerId, + channel_id: config.storeChannelId, + redirect_to: data.checkout_url, + } + let token = jwt.sign(payload, config.storeApiClientSecret!, { + algorithm: 'HS256', + }) + let checkouturl = `${config.storeUrl}/login/token/${token}` + console.log('checkouturl', checkouturl) + if (fullCheckout) { + res.redirect(checkouturl) + return + } } // TODO: make the embedded checkout work too! diff --git a/framework/bigcommerce/api/endpoints/wishlist/add-item.ts b/framework/bigcommerce/api/endpoints/wishlist/add-item.ts index 4c5970a5d..bf449cb11 100644 --- a/framework/bigcommerce/api/endpoints/wishlist/add-item.ts +++ b/framework/bigcommerce/api/endpoints/wishlist/add-item.ts @@ -1,6 +1,6 @@ import getCustomerWishlist from '../../operations/get-customer-wishlist' import { parseWishlistItem } from '../../utils/parse-item' -import getCustomerId from './utils/get-customer-id' +import getCustomerId from '../../utils/get-customer-id' import type { WishlistEndpoint } from '.' // Return wishlist info diff --git a/framework/bigcommerce/api/endpoints/wishlist/get-wishlist.ts b/framework/bigcommerce/api/endpoints/wishlist/get-wishlist.ts index 21443119c..479a67c6e 100644 --- a/framework/bigcommerce/api/endpoints/wishlist/get-wishlist.ts +++ b/framework/bigcommerce/api/endpoints/wishlist/get-wishlist.ts @@ -1,6 +1,6 @@ import type { Wishlist } from '../../../types/wishlist' import type { WishlistEndpoint } from '.' -import getCustomerId from './utils/get-customer-id' +import getCustomerId from '../../utils/get-customer-id' import getCustomerWishlist from '../../operations/get-customer-wishlist' // Return wishlist info diff --git a/framework/bigcommerce/api/endpoints/wishlist/remove-item.ts b/framework/bigcommerce/api/endpoints/wishlist/remove-item.ts index 22ac31cf9..9b19d9b42 100644 --- a/framework/bigcommerce/api/endpoints/wishlist/remove-item.ts +++ b/framework/bigcommerce/api/endpoints/wishlist/remove-item.ts @@ -1,6 +1,6 @@ import type { Wishlist } from '../../../types/wishlist' import getCustomerWishlist from '../../operations/get-customer-wishlist' -import getCustomerId from './utils/get-customer-id' +import getCustomerId from '../../utils/get-customer-id' import type { WishlistEndpoint } from '.' // Return wishlist info diff --git a/framework/bigcommerce/api/index.ts b/framework/bigcommerce/api/index.ts index f28f2a1f4..9dbe400f9 100644 --- a/framework/bigcommerce/api/index.ts +++ b/framework/bigcommerce/api/index.ts @@ -32,6 +32,9 @@ export interface BigcommerceConfig extends CommerceAPIConfig { storeApiToken: string storeApiClientId: string storeChannelId?: string + storeUrl?: string + storeApiClientSecret?: string + storeHash?:string storeApiFetch(endpoint: string, options?: RequestInit): Promise } @@ -41,6 +44,9 @@ const STORE_API_URL = process.env.BIGCOMMERCE_STORE_API_URL const STORE_API_TOKEN = process.env.BIGCOMMERCE_STORE_API_TOKEN const STORE_API_CLIENT_ID = process.env.BIGCOMMERCE_STORE_API_CLIENT_ID const STORE_CHANNEL_ID = process.env.BIGCOMMERCE_CHANNEL_ID +const STORE_URL = process.env.BIGCOMMERCE_STORE_URL +const CLIENT_SECRET = process.env.BIGCOMMERCE_STORE_API_CLIENT_SECRET +const STOREFRONT_HASH = process.env.BIGCOMMERCE_STORE_API_STORE_HASH if (!API_URL) { throw new Error( @@ -75,6 +81,9 @@ const config: BigcommerceConfig = { storeApiToken: STORE_API_TOKEN, storeApiClientId: STORE_API_CLIENT_ID, storeChannelId: STORE_CHANNEL_ID, + storeUrl:STORE_URL, + storeApiClientSecret:CLIENT_SECRET, + storeHash: STOREFRONT_HASH, storeApiFetch: createFetchStoreApi(() => getCommerceApi().getConfig()), } diff --git a/framework/bigcommerce/api/endpoints/wishlist/utils/get-customer-id.ts b/framework/bigcommerce/api/utils/get-customer-id.ts similarity index 83% rename from framework/bigcommerce/api/endpoints/wishlist/utils/get-customer-id.ts rename to framework/bigcommerce/api/utils/get-customer-id.ts index 603f8be2d..7efeeed3c 100644 --- a/framework/bigcommerce/api/endpoints/wishlist/utils/get-customer-id.ts +++ b/framework/bigcommerce/api/utils/get-customer-id.ts @@ -1,5 +1,5 @@ -import type { GetCustomerIdQuery } from '../../../../schema' -import type { BigcommerceConfig } from '../../..' +import type { GetCustomerIdQuery } from '../../schema' +import type { BigcommerceConfig } from '../' export const getCustomerIdQuery = /* GraphQL */ ` query getCustomerId { diff --git a/next-env.d.ts b/next-env.d.ts index 7b7aa2c77..c6643fda1 100644 --- a/next-env.d.ts +++ b/next-env.d.ts @@ -1,2 +1,3 @@ /// /// +/// diff --git a/package.json b/package.json index 7b7336baa..95b46c497 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,8 @@ "swell-js": "^4.0.0-next.0", "swr": "^0.5.6", "tabbable": "^5.2.0", - "tailwindcss": "^2.2.2" + "tailwindcss": "^2.2.2", + "uuidv4": "^6.2.10" }, "devDependencies": { "@graphql-codegen/cli": "^1.21.5",