Skip to content

Commit 97bea08

Browse files
committed
refactor(csrf): simplify token handling
1 parent d87a3b2 commit 97bea08

File tree

4 files changed

+22
-30
lines changed

4 files changed

+22
-30
lines changed

frontend/.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"a11y",
1313
"checkout",
1414
"ci",
15+
"csrf",
1516
"customFetch",
1617
"dialog",
1718
"eslint",

frontend/composables/useCustomFetch.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ export function useCustomFetch() {
1515
const xForwardedFor = useRequestHeader('x-forwarded-for')
1616
const xRealIp = useRequestHeader('x-real-ip')
1717
const {
18-
csrfHeader, setCsrfHeader,
18+
setTokenCsrf,
19+
tokenCsrf,
1920
} = useCsrfStore()
2021
const { showError } = useBcToast()
2122
const { t: $t } = useTranslation()
2223
const { $bcLogger } = useNuxtApp()
2324
const uuid = inject<{ value: string }>('app-uuid')
24-
2525
async function fetch<T>(
2626
pathName: PathName,
2727
// eslint-disable-next-line @typescript-eslint/ban-types
@@ -100,8 +100,11 @@ export function useCustomFetch() {
100100

101101
// For non GET method's we need to set the csrf header for security
102102
if (method !== 'GET') {
103-
if (csrfHeader.value) {
104-
options.headers.append(csrfHeader.value[0], csrfHeader.value[1])
103+
if (tokenCsrf.value) {
104+
options.headers = new Headers({
105+
...options.headers,
106+
'x-csrf-token': tokenCsrf.value,
107+
})
105108
}
106109
else {
107110
$bcLogger.warn(`${uuid?.value} | missing csrf header!`)
@@ -116,7 +119,6 @@ export function useCustomFetch() {
116119
})
117120
return res as T
118121
}
119-
120122
try {
121123
const res = await $fetch.raw<T>(path, {
122124
baseURL,
@@ -125,7 +127,10 @@ export function useCustomFetch() {
125127
})
126128
if (method === 'GET') {
127129
// We get the csrf header from GET requests
128-
setCsrfHeader(res.headers)
130+
const tokenCsrf = res.headers.get('x-csrf-token')
131+
if (tokenCsrf) {
132+
setTokenCsrf(tokenCsrf)
133+
}
129134
}
130135
return res._data as T
131136
}
@@ -141,5 +146,7 @@ export function useCustomFetch() {
141146
}
142147
}
143148

144-
return { fetch }
149+
return {
150+
fetch,
151+
}
145152
}

frontend/stores/useCsrfStore.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
import { defineStore } from 'pinia'
2-
import { getCSRFHeader } from '~/utils/fetch'
32

43
/**
54
The csrf header is added to non GET requests to prevent Cross-site request forgery
65
We get the csrf header from GET requests put them in this store and apply them to non GET requests.
76
**/
87

98
const csrfStore = defineStore('csrf_store', () => {
10-
const header = ref<[string, string] | null | undefined>()
11-
return { header }
9+
const tokenCsrf = ref<string >('')
10+
return { tokenCsrf }
1211
})
1312

1413
export function useCsrfStore() {
15-
const { header } = storeToRefs(csrfStore())
14+
const { tokenCsrf } = storeToRefs(csrfStore())
1615

17-
const csrfHeader = computed(() => header.value)
18-
19-
function setCsrfHeader(headers: Headers) {
20-
const h = getCSRFHeader(headers)
21-
if (h) {
22-
header.value = h
23-
}
16+
function setTokenCsrf(token: string) {
17+
tokenCsrf.value = token
2418
}
2519

2620
return {
27-
csrfHeader,
28-
setCsrfHeader,
21+
setTokenCsrf,
22+
tokenCsrf,
2923
}
3024
}

frontend/utils/fetch.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)