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)
|
const cookieHandler = new CookieHandler(config, req, res)
|
||||||
let accessToken = cookieHandler.getAccessToken();
|
let accessToken = cookieHandler.getAccessToken();
|
||||||
|
|
||||||
if (cookieHandler.getAccessToken()) {
|
if (!cookieHandler.isShopperCookieAnonymous()) {
|
||||||
const { data } = await config.fetch(getCustomerAccountQuery, undefined, {
|
const { data } = await config.fetch(getCustomerAccountQuery, undefined, {
|
||||||
headers: {
|
headers: {
|
||||||
'x-vol-user-claims': accessToken,
|
'x-vol-user-claims': accessToken,
|
||||||
|
@ -5,6 +5,11 @@ import { setCookies } from '../../lib/set-cookie'
|
|||||||
import { NextApiRequest } from 'next'
|
import { NextApiRequest } from 'next'
|
||||||
import getAnonymousShopperToken from './get-anonymous-shopper-token'
|
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 {
|
export default class CookieHandler {
|
||||||
config: KiboCommerceConfig
|
config: KiboCommerceConfig
|
||||||
request: NextApiRequest
|
request: NextApiRequest
|
||||||
@ -15,9 +20,7 @@ export default class CookieHandler {
|
|||||||
this.request = req
|
this.request = req
|
||||||
this.response = res
|
this.response = res
|
||||||
const encodedToken = req.cookies[config.customerCookie]
|
const encodedToken = req.cookies[config.customerCookie]
|
||||||
const token = encodedToken
|
const token = parseCookie(encodedToken)
|
||||||
? JSON.parse(Buffer.from(encodedToken, 'base64').toString('ascii'))
|
|
||||||
: null
|
|
||||||
this.accessToken = token ? token.accessToken : null
|
this.accessToken = token ? token.accessToken : null
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,7 +34,13 @@ export default class CookieHandler {
|
|||||||
accessToken: anonymousAccessToken,
|
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) {
|
setAnonymousShopperCookie(anonymousShopperTokenResponse: any) {
|
||||||
const cookieExpirationDate = getCookieExpirationDate(
|
const cookieExpirationDate = getCookieExpirationDate(
|
||||||
this.config.customerCookieMaxAgeInDays
|
this.config.customerCookieMaxAgeInDays
|
||||||
|
Loading…
x
Reference in New Issue
Block a user