Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slow-cooks-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@scaleway/cookie-consent": patch
---

fix usage of cookies
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cookie from 'cookie'
import { parse, serialize } from 'cookie'
import type { SerializeOptions } from 'cookie'
import {
createContext,
Expand All @@ -11,13 +11,12 @@ import {
import type { PropsWithChildren } from 'react'
import { uniq } from '../helpers/array'
import { stringToHash } from '../helpers/misc'
import { isCategoryKind } from './helpers'
import { IS_CLIENT, isCategoryKind } from './helpers'
import type { Config, Consent, Integrations } from './types'
import { useSegmentIntegrations } from './useSegmentIntegrations'

const COOKIE_PREFIX = '_scw_rgpd'
const HASH_COOKIE = `${COOKIE_PREFIX}_hash`
const IS_CLIENT = typeof document !== 'undefined'

// Appx 13 Months
const CONSENT_MAX_AGE = 13 * 30 * 24 * 60 * 60
Expand Down Expand Up @@ -73,7 +72,7 @@ export const CookieConsentProvider = ({
}>) => {
const [needConsent, setNeedsConsent] = useState(false)
const [cookies, setCookies] = useState<Record<string, string | undefined>>(
IS_CLIENT ? cookie.parse(document.cookie) : {},
IS_CLIENT ? parse(document.cookie) : {},
)

const {
Expand Down Expand Up @@ -160,38 +159,30 @@ export const CookieConsentProvider = ({

if (!consentValue) {
// If consent is set to false we have to delete the cookie
document.cookie = cookie.serialize(cookieName, '', {
document.cookie = serialize(cookieName, '', {
...cookiesOptions,
expires: new Date(0),
})
} else {
document.cookie = cookie.serialize(
cookieName,
consentValue.toString(),
{
...cookiesOptions,
maxAge:
consentCategoryName === 'advertising'
? consentAdvertisingMaxAge
: consentMaxAge,
},
)
document.cookie = serialize(cookieName, consentValue.toString(), {
...cookiesOptions,
maxAge:
consentCategoryName === 'advertising'
? consentAdvertisingMaxAge
: consentMaxAge,
})
}
setCookies(prevCookies => ({
...prevCookies,
[cookieName]: consentValue ? 'true' : 'false',
}))
}
// We set the hash cookie to the current consented integrations
document.cookie = cookie.serialize(
HASH_COOKIE,
integrationsHash.toString(),
{
...cookiesOptions,
// Here we use the shortest max age to force to ask again for expired consent
maxAge: consentAdvertisingMaxAge,
},
)
document.cookie = serialize(HASH_COOKIE, integrationsHash.toString(), {
...cookiesOptions,
// Here we use the shortest max age to force to ask again for expired consent
maxAge: consentAdvertisingMaxAge,
})
setCookies(prevCookies => ({
...prevCookies,
[HASH_COOKIE]: integrationsHash.toString(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useSegment } from '@scaleway/use-segment'
import cookie from 'cookie'
import { parse } from 'cookie'
import type { PropsWithChildren } from 'react'
import { useCookieConsent } from './CookieConsentProvider'
import { type CategoryKind } from './helpers'
import { IS_CLIENT } from './helpers'
import type { CategoryKind } from './helpers'

export const AMPLITUDE_INTEGRATION_NAME = 'Amplitude (Actions)'
const COOKIE_SESSION_ID_NAME = 'analytics_session_id'
Expand All @@ -14,7 +15,9 @@ type ConsentObject = {
}

export const getSessionId = () => {
const sessionId = cookie.parse(document.cookie)[COOKIE_SESSION_ID_NAME]
const sessionId = IS_CLIENT
? parse(document.cookie)[COOKIE_SESSION_ID_NAME]
: ''
if (sessionId) {
return Number.parseInt(sessionId, 10)
}
Expand Down
2 changes: 2 additions & 0 deletions packages/cookie-consent/src/CookieConsentProvider/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ export const categories = [

export type CategoryKind = (typeof categories)[number]

export const IS_CLIENT = typeof document !== 'undefined'

export const isCategoryKind = (key: string): key is CategoryKind =>
categories.includes(key as CategoryKind)
Loading