Skip to content

Commit

Permalink
feat: encode shipping info to base64
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurFerrao committed Jan 17, 2025
1 parent 7a79e83 commit 60772ae
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
6 changes: 2 additions & 4 deletions react/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { SHIPPING_INFO_COOKIE } from './constants'
import { setCookie } from './utils/cookie'

export const ALL_SEMICOLON_REGEX = /;/g

export const getAddress = (
countryCode: string,
zipCode: string,
Expand All @@ -22,8 +20,8 @@ export const updateSession = (
)}${pickup ? `;pickupPoint=${pickup.pickupPoint.id}` : ''}`

// __RUNTIME__.segmentToken is not reliable for the facets. It might not be updated. For this reason we must try to get the info from our custom cookie first
// Replacing ";" by ":" because ";" is not allowed in cookies
setCookie(SHIPPING_INFO_COOKIE, facetsValue.replace(ALL_SEMICOLON_REGEX, ':'))
// Encode to base64 because ";" is not allowed in cookies
setCookie(SHIPPING_INFO_COOKIE, btoa(facetsValue))

return fetch('/api/sessions', {
method: 'POST',
Expand Down
10 changes: 2 additions & 8 deletions react/utils/cookie.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { SHIPPING_INFO_COOKIE } from '../constants'

export const ALL_COLON_REGEX = /:/g

export function getCookie(name: string) {
const value = `; ${document.cookie}`
const parts = value.split(`; ${name}=`)
Expand All @@ -25,12 +23,10 @@ export function setCookie(name: string, val: string) {
export function getFacetsData(facetsDataTarget: string) {
// __RUNTIME__.segmentToken is not reliable for the facets. It might not be updated. For this reason we must try to get the info from our custom cookie first

let facets = getCookie(SHIPPING_INFO_COOKIE)
let facets = atob(getCookie(SHIPPING_INFO_COOKIE) ?? '')

if (!facets) {
const segment =
getCookie(SHIPPING_INFO_COOKIE) ??
(window as any)?.__RUNTIME__.segmentToken
const segment = (window as any)?.__RUNTIME__.segmentToken

if (!segment) {
return
Expand All @@ -43,9 +39,7 @@ export function getFacetsData(facetsDataTarget: string) {
return
}

// In case the facets came from the shipping_info cookie we must replace ":" by ";" because ";" is not allowed in cookies.
const facetsTarget = facets
.replace(ALL_COLON_REGEX, ';')
.split(';')
.find((facet: string) => facet.indexOf(facetsDataTarget) > -1)

Expand Down

0 comments on commit 60772ae

Please sign in to comment.