mirror of
https://github.com/vercel/commerce.git
synced 2025-07-24 10:41:23 +00:00
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.
This commit is contained in:
@@ -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,21 +12,44 @@ const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({
|
||||
}) => {
|
||||
const { cookies } = req
|
||||
const cartId = cookies[config.cartCookie]
|
||||
const customerToken = cookies[config.customerCookie]
|
||||
let checkouturl: string
|
||||
|
||||
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 there is a customer create a jwt token
|
||||
if (customerId >= 0) {
|
||||
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',
|
||||
})
|
||||
checkouturl = `${config.storeUrl}/login/token/${token}`
|
||||
} else {
|
||||
checkouturl = data.checkout_url
|
||||
}
|
||||
|
||||
if (fullCheckout) {
|
||||
res.redirect(data.checkout_url)
|
||||
res.redirect(checkouturl)
|
||||
return
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user