Skip to content

Commit 6789326

Browse files
authored
fix(cookies): usage of cookie package (#2378)
Signed-off-by: Alexandre Philibeaux <[email protected]>
1 parent e4002cd commit 6789326

File tree

5 files changed

+41
-39
lines changed

5 files changed

+41
-39
lines changed

.changeset/poor-garlics-flash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@scaleway/cookie-consent": patch
3+
---
4+
5+
update import cookies

packages/cookie-consent/src/CookieConsentProvider/CookieConsentProvider.tsx

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import cookie from 'cookie'
1+
import { parse, serialize } from 'cookie'
22
import type { SerializeOptions } from 'cookie'
33
import {
44
createContext,
@@ -11,13 +11,12 @@ import {
1111
import type { PropsWithChildren } from 'react'
1212
import { uniq } from '../helpers/array'
1313
import { stringToHash } from '../helpers/misc'
14-
import { isCategoryKind } from './helpers'
14+
import { IS_CLIENT, isCategoryKind } from './helpers'
1515
import type { Config, Consent, Integrations } from './types'
1616
import { useSegmentIntegrations } from './useSegmentIntegrations'
1717

1818
const COOKIE_PREFIX = '_scw_rgpd'
1919
const HASH_COOKIE = `${COOKIE_PREFIX}_hash`
20-
const IS_CLIENT = typeof document !== 'undefined'
2120

2221
// Appx 13 Months
2322
const CONSENT_MAX_AGE = 13 * 30 * 24 * 60 * 60
@@ -72,8 +71,9 @@ export const CookieConsentProvider = ({
7271
cookiesOptions?: SerializeOptions
7372
}>) => {
7473
const [needConsent, setNeedsConsent] = useState(false)
74+
console.debug('document cookie', document.cookie)
7575
const [cookies, setCookies] = useState<Record<string, string | undefined>>(
76-
IS_CLIENT ? cookie.parse(document.cookie) : {},
76+
IS_CLIENT ? parse(document.cookie) : {},
7777
)
7878

7979
const {
@@ -113,6 +113,7 @@ export const CookieConsentProvider = ({
113113
// We set needConsent at false until we have an answer from segment
114114
// This is to avoid showing setting needConsent to true only to be set
115115
// to false after receiving segment answer and flicker the UI
116+
116117
setNeedsConsent(
117118
isConsentRequired &&
118119
cookies[HASH_COOKIE] !== integrationsHash.toString() &&
@@ -160,38 +161,30 @@ export const CookieConsentProvider = ({
160161

161162
if (!consentValue) {
162163
// If consent is set to false we have to delete the cookie
163-
document.cookie = cookie.serialize(cookieName, '', {
164+
document.cookie = serialize(cookieName, '', {
164165
...cookiesOptions,
165166
expires: new Date(0),
166167
})
167168
} else {
168-
document.cookie = cookie.serialize(
169-
cookieName,
170-
consentValue.toString(),
171-
{
172-
...cookiesOptions,
173-
maxAge:
174-
consentCategoryName === 'advertising'
175-
? consentAdvertisingMaxAge
176-
: consentMaxAge,
177-
},
178-
)
169+
document.cookie = serialize(cookieName, consentValue.toString(), {
170+
...cookiesOptions,
171+
maxAge:
172+
consentCategoryName === 'advertising'
173+
? consentAdvertisingMaxAge
174+
: consentMaxAge,
175+
})
179176
}
180177
setCookies(prevCookies => ({
181178
...prevCookies,
182179
[cookieName]: consentValue ? 'true' : 'false',
183180
}))
184181
}
185182
// We set the hash cookie to the current consented integrations
186-
document.cookie = cookie.serialize(
187-
HASH_COOKIE,
188-
integrationsHash.toString(),
189-
{
190-
...cookiesOptions,
191-
// Here we use the shortest max age to force to ask again for expired consent
192-
maxAge: consentAdvertisingMaxAge,
193-
},
194-
)
183+
document.cookie = serialize(HASH_COOKIE, integrationsHash.toString(), {
184+
...cookiesOptions,
185+
// Here we use the shortest max age to force to ask again for expired consent
186+
maxAge: consentAdvertisingMaxAge,
187+
})
195188
setCookies(prevCookies => ({
196189
...prevCookies,
197190
[HASH_COOKIE]: integrationsHash.toString(),

packages/cookie-consent/src/CookieConsentProvider/SegmentConsentMiddleware.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { useSegment } from '@scaleway/use-segment'
2-
import cookie from 'cookie'
2+
import { parse } from 'cookie'
33
import type { PropsWithChildren } from 'react'
44
import { useCookieConsent } from './CookieConsentProvider'
5+
import { IS_CLIENT } from './helpers'
56
import { type CategoryKind } from './helpers'
67

78
export const AMPLITUDE_INTEGRATION_NAME = 'Amplitude (Actions)'
@@ -14,7 +15,9 @@ type ConsentObject = {
1415
}
1516

1617
export const getSessionId = () => {
17-
const sessionId = cookie.parse(document.cookie)[COOKIE_SESSION_ID_NAME]
18+
const sessionId = IS_CLIENT
19+
? parse(document.cookie)[COOKIE_SESSION_ID_NAME]
20+
: ''
1821
if (sessionId) {
1922
return Number.parseInt(sessionId, 10)
2023
}

packages/cookie-consent/src/CookieConsentProvider/__tests__/index.test.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// useSegmentIntegrations tests have been splitted in multiple files because of https://github.com/facebook/vi/issues/8987
2-
import { act, renderHook } from '@testing-library/react'
3-
import cookie from 'cookie'
2+
import { act, renderHook, waitFor } from '@testing-library/react'
3+
import * as cookie from 'cookie'
44
import type { ComponentProps, ReactNode } from 'react'
55
import { describe, expect, it, vi } from 'vitest'
66
import { CookieConsentProvider, useCookieConsent } from '..'
@@ -193,9 +193,9 @@ describe('CookieConsent - CookieConsentProvider', () => {
193193
})
194194

195195
it('should not need consent if hash cookie is set', () => {
196-
vi.spyOn(cookie, 'parse').mockImplementation(() => ({
197-
_scw_rgpd_hash: '913003917',
198-
}))
196+
// document.cookie = '_scw_rgpd_hash=913003917;'
197+
document.cookie = cookie.serialize('_scw_rgpd_hash', '913003917')
198+
199199
const { result } = renderHook(() => useCookieConsent(), {
200200
wrapper: wrapper({
201201
isConsentRequired: true,
@@ -215,19 +215,18 @@ describe('CookieConsent - CookieConsentProvider', () => {
215215
})
216216
})
217217

218-
it('should not need consent if hash cookie is set and some categories already approved', () => {
219-
vi.spyOn(cookie, 'parse').mockImplementation(() => ({
220-
_scw_rgpd_hash: '913003917',
221-
_scw_rgpd_marketing: 'true',
222-
}))
218+
it('should not need consent if hash cookie is set and some categories already approved', async () => {
219+
document.cookie = cookie.serialize('_scw_rgpd_marketing', 'true')
220+
document.cookie = cookie.serialize('_scw_rgpd_hash', '913003917')
223221

224222
const { result } = renderHook(() => useCookieConsent(), {
225223
wrapper: wrapper({
226224
isConsentRequired: true,
227225
}),
228226
})
229-
230-
expect(result.current.needConsent).toBe(false)
227+
await waitFor(() => {
228+
expect(result.current.needConsent).toBe(false)
229+
})
231230
expect(result.current.isSegmentAllowed).toBe(true)
232231
expect(result.current.categoriesConsent).toStrictEqual({
233232
analytics: false,

packages/cookie-consent/src/CookieConsentProvider/helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ export type CategoryKind = (typeof categories)[number]
1010

1111
export const isCategoryKind = (key: string): key is CategoryKind =>
1212
categories.includes(key as CategoryKind)
13+
14+
export const IS_CLIENT = typeof document !== 'undefined'

0 commit comments

Comments
 (0)