|
1 | 1 | // Adapted from https://github.com/nuxt/nuxt/blob/7046930a677f4c987afaee5a0165841b0e0a517f/packages/nuxt/src/app/composables/fetch.ts |
2 | | -import { useFetch, type FetchResult, type UseFetchOptions, useNuxtApp } from '#app' |
| 2 | +import { useFetch, type FetchResult, type UseFetchOptions, useNuxtApp , useRuntimeConfig } from '#app' |
3 | 3 | import type { Ref } from 'vue' |
4 | 4 | import type { FetchError } from 'ofetch' |
5 | 5 | import type { NitroFetchRequest, AvailableRouterMethod as _AvailableRouterMethod } from 'nitropack' |
@@ -49,9 +49,9 @@ export function useCsrfFetch< |
49 | 49 | arg2?: string |
50 | 50 | ) { |
51 | 51 | const [opts = {}, autoKey] = typeof arg1 === 'string' ? [{}, arg1] : [arg1, arg2] |
52 | | - const { csrf } = useCsrf() |
| 52 | + const { csrf, headerName } = useCsrf() |
53 | 53 | opts.headers = (opts.headers || {}) as Record<string, string> |
54 | | - opts.headers['csrf-token'] = csrf // add csrf token to req headers |
| 54 | + opts.headers[headerName] = csrf // add csrf token to req headers |
55 | 55 | return useFetch<ResT, ErrorT, ReqT, Method, _ResT, DataT, PickKeys, DefaultT>( |
56 | 56 | request, |
57 | 57 | opts, |
@@ -111,9 +111,10 @@ export function useLazyCsrfFetch< |
111 | 111 | } |
112 | 112 |
|
113 | 113 | export function useCsrf() { |
| 114 | + const headerName = useRuntimeConfig().public.csurf.headerName ?? '' |
114 | 115 | if (import.meta.server) { |
115 | | - return { csrf: useNuxtApp().ssrContext?.event?.context?.csrfToken } |
| 116 | + return { csrf: useNuxtApp().ssrContext?.event?.context?.csrfToken, headerName } |
116 | 117 | } |
117 | 118 | const metaTag = window.document.querySelector('meta[name="csrf-token"]') |
118 | | - return { csrf: metaTag?.getAttribute('content') } |
| 119 | + return { csrf: metaTag?.getAttribute('content'), headerName } |
119 | 120 | } |
0 commit comments