-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathnodeCookies.ts
More file actions
59 lines (55 loc) · 1.52 KB
/
nodeCookies.ts
File metadata and controls
59 lines (55 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copied from https://github.com/research-software-directory/RSD-as-a-service/blob/main/frontend/components/cookies/nodeCookies.ts
import { IncomingMessage } from 'http'
import cookie from 'cookie'
export type Matomo = {
id: string | null
consent: boolean | null
}
/**
* Extract Matomo cookies
* Available only on the server side, using plain Node request
* @param req
* @returns Session
*/
export function getMatomoCookies (req: IncomingMessage) {
// check for cookies
if (req?.headers?.cookie) {
// parse cookies from node request
const cookies = cookie.parse(req.headers.cookie)
// validate and decode
return {
mtm_consent: cookies?.mtm_consent,
mtm_consent_removed: cookies?.mtm_consent_removed
}
} else {
return {
mtm_consent: undefined,
mtm_consent_removed: undefined
}
}
}
/**
* Extract matomo consent based on cookies
* Available only on the server side, using plain Node request
* @param req
* @returns Session
*/
export function getMatomoConsent (req:IncomingMessage) {
// check for cookies
// console.log('getMatomoConsent...', req)
if (req?.headers?.cookie) {
const matomo = getMatomoCookies(req)
if (matomo?.mtm_consent) {
return {
matomoConsent: true
}
} else if (matomo?.mtm_consent_removed) {
return {
matomoConsent: false
}
}
}
return {
matomoConsent: null
}
}