4
0
forked from crowetic/commerce

fix(ckibocommerce customer endpoint): check if shopper cookie is anonymous before calling kibo api (#613)

This commit is contained in:
kibo-kevinwatts 2021-12-16 08:02:46 -06:00 committed by GitHub
parent 2271864057
commit 3a69dbb5b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View File

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

View File

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