This repository was archived by the owner on Apr 6, 2023. It is now read-only.
File tree 1 file changed +8
-12
lines changed
docs/content/2.guide/2.features 1 file changed +8
-12
lines changed Original file line number Diff line number Diff line change @@ -212,24 +212,20 @@ Here is a list of common headers that are NOT to be proxied:
212
212
If you want to pass on/proxy cookies in the other direction, from an internal request back to the client, you will need to handle this yourself.
213
213
214
214
``` ts [composables/fetch.ts]
215
- export const fetchWithCookie = async (url : string , cookieName : string ) => {
216
- const response = await $fetch .raw (url )
217
- if (process .server ) {
218
- const cookies = Object .fromEntries (
219
- response .headers .get (' set-cookie' )?.split (' ; ' ).map ((a ) => a .split (' =' ))
220
- )
221
- if (cookieName in cookies ) {
222
- useCookie (cookieName ).value = cookies [cookieName ]
223
- }
215
+ export const fetchWithCookie = async (url : string ) => {
216
+ const res = await $fetch .raw (url )
217
+ const cookies = (res .headers .get (' set-cookie' ) || ' ' ).split (' ,' )
218
+ for (const cookie of cookies ) {
219
+ appendHeader (useRequestEvent (), ' set-cookie' , cookie )
224
220
}
225
- return response ._data
221
+ return res ._data
226
222
}
227
223
```
228
224
229
225
``` vue
230
226
<script setup lang="ts">
231
- // This composable will automatically pass on a cookie of our choice.
232
- const result = await fetchWithCookie("/api/with-cookie", "test" )
227
+ // This composable will automatically pass cookies to the client
228
+ const result = await fetchWithCookie("/api/with-cookie")
233
229
onMounted(() => console.log(document.cookie))
234
230
</script>
235
231
```
You can’t perform that action at this time.
0 commit comments