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 <george.fitzgibbons@c02zw1aqlvdn.lan>
This commit is contained in:
George FitzGibbons
2021-07-01 12:21:01 -04:00
committed by GitHub
parent a314893f62
commit 166bb037e4
9 changed files with 54 additions and 11 deletions

View File

@@ -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!

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -1,32 +0,0 @@
import type { GetCustomerIdQuery } from '../../../../schema'
import type { BigcommerceConfig } from '../../..'
export const getCustomerIdQuery = /* GraphQL */ `
query getCustomerId {
customer {
entityId
}
}
`
async function getCustomerId({
customerToken,
config,
}: {
customerToken: string
config: BigcommerceConfig
}): Promise<string | undefined> {
const { data } = await config.fetch<GetCustomerIdQuery>(
getCustomerIdQuery,
undefined,
{
headers: {
cookie: `${config.customerCookie}=${customerToken}`,
},
}
)
return String(data?.customer?.entityId)
}
export default getCustomerId