forked from crowetic/commerce
fix(ckibocommerce customer endpoint): check if shopper cookie is anonymous before calling kibo api (#613)
This commit is contained in:
parent
2271864057
commit
3a69dbb5b0
@ -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,
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user