Skip to content

Commit

Permalink
fix(ckibocommerce customer endpoint): check if shopper cookie is anon…
Browse files Browse the repository at this point in the history
…ymous before calling kibo api (vercel#613)
  • Loading branch information
kibo-kevinwatts authored Dec 16, 2021
1 parent 2271864 commit 3a69dbb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion framework/kibocommerce/api/endpoints/customer/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const getLoggedInCustomer: CustomerEndpoint['handlers']['getLoggedInCustomer'] =
const cookieHandler = new CookieHandler(config, req, res)
let accessToken = cookieHandler.getAccessToken();

if (cookieHandler.getAccessToken()) {
if (!cookieHandler.isShopperCookieAnonymous()) {
const { data } = await config.fetch(getCustomerAccountQuery, undefined, {
headers: {
'x-vol-user-claims': accessToken,
Expand Down
17 changes: 13 additions & 4 deletions framework/kibocommerce/api/utils/cookie-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { setCookies } from '../../lib/set-cookie'
import { NextApiRequest } from 'next'
import getAnonymousShopperToken from './get-anonymous-shopper-token'

const parseCookie = (cookieValue?: any) => {
return cookieValue
? JSON.parse(Buffer.from(cookieValue, 'base64').toString('ascii'))
: null
}
export default class CookieHandler {
config: KiboCommerceConfig
request: NextApiRequest
Expand All @@ -15,9 +20,7 @@ export default class CookieHandler {
this.request = req
this.response = res
const encodedToken = req.cookies[config.customerCookie]
const token = encodedToken
? JSON.parse(Buffer.from(encodedToken, 'base64').toString('ascii'))
: null
const token = parseCookie(encodedToken)
this.accessToken = token ? token.accessToken : null
}

Expand All @@ -31,7 +34,13 @@ export default class CookieHandler {
accessToken: anonymousAccessToken,
}
}

isShopperCookieAnonymous() {
const customerCookieKey = this.config.customerCookie
const shopperCookie = this.request.cookies[customerCookieKey]
const shopperSession = parseCookie(shopperCookie);
const isAnonymous = shopperSession?.customerAccount ? false : true
return isAnonymous
}
setAnonymousShopperCookie(anonymousShopperTokenResponse: any) {
const cookieExpirationDate = getCookieExpirationDate(
this.config.customerCookieMaxAgeInDays
Expand Down

0 comments on commit 3a69dbb

Please sign in to comment.