Skip to content

Commit 2e1681d

Browse files
janus/segment: extract session cookie in a safe way
1 parent 2cf626e commit 2e1681d

2 files changed

Lines changed: 4 additions & 9 deletions

File tree

src/clients/Segment.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import parseCookie from 'cookie'
21
import { prop } from 'ramda'
32

43
import { PRODUCT_HEADER } from '../constants'
54
import { inflightUrlWithQuery, JanusClient } from '../HttpClient'
5+
import { extractSessionCookie } from './Session'
66

77
export interface SegmentData {
88
campaigns?: any
@@ -19,7 +19,6 @@ export interface SegmentData {
1919
[key: string]: any
2020
}
2121

22-
const SEGMENT_COOKIE = 'vtex_segment'
2322
const SEGMENT_MAX_AGE_S = 60 * 60 // 60 minutes - segment is actually immutable
2423
const ALLOWED_QUERY_PREFIXES = ['utm', 'cultureInfo', 'supportedLocales']
2524

@@ -71,15 +70,11 @@ export class Segment extends JanusClient {
7170
public getOrCreateSegment = async (query?: Record<string, string>, token?: string) => {
7271
const {
7372
data: segmentData,
74-
headers: {
75-
'set-cookie': [setCookies],
76-
},
73+
headers,
7774
} = await this.rawSegment(token, query)
78-
const parsedCookie = parseCookie.parse(setCookies)
79-
const segmentToken = prop(SEGMENT_COOKIE, parsedCookie)
8075
return {
8176
segmentData,
82-
segmentToken,
77+
segmentToken: extractSessionCookie(headers) || token,
8378
}
8479
}
8580

src/clients/Session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class Session extends JanusClient {
5151
}
5252
}
5353

54-
function extractSessionCookie(headers: Record<string, string>) {
54+
export function extractSessionCookie(headers: Record<string, string>) {
5555
for (const setCookie of headers['set-cookie'] || []) {
5656
const parsedCookie = parseCookie.parse(setCookie)
5757
const sessionCookie = parsedCookie[SESSION_COOKIE]

0 commit comments

Comments
 (0)