Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
091537b9
Commit
091537b9
authored
Dec 24, 2022
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pass csrf token in header instead of body
parent
b242508b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
8 additions
and
2 deletions
+8
-2
fetch.ts
lib/api/fetch.ts
+2
-0
useFetch.tsx
lib/hooks/useFetch.tsx
+6
-2
No files found.
lib/api/fetch.ts
View file @
091537b9
...
@@ -13,10 +13,12 @@ export default function fetchFactory(
...
@@ -13,10 +13,12 @@ export default function fetchFactory(
apiEndpoint
:
string
=
appConfig
.
api
.
endpoint
,
apiEndpoint
:
string
=
appConfig
.
api
.
endpoint
,
)
{
)
{
return
function
fetch
(
path
:
string
,
init
?:
RequestInit
):
Promise
<
Response
>
{
return
function
fetch
(
path
:
string
,
init
?:
RequestInit
):
Promise
<
Response
>
{
const
csrfToken
=
_req
.
headers
[
'
x-csrf-token
'
]?.
toString
();
const
headers
=
{
const
headers
=
{
accept
:
'
application/json
'
,
accept
:
'
application/json
'
,
'
content-type
'
:
'
application/json
'
,
'
content-type
'
:
'
application/json
'
,
cookie
:
`
${
cookies
.
NAMES
.
API_TOKEN
}
=
${
_req
.
cookies
[
cookies
.
NAMES
.
API_TOKEN
]
}
`
,
cookie
:
`
${
cookies
.
NAMES
.
API_TOKEN
}
=
${
_req
.
cookies
[
cookies
.
NAMES
.
API_TOKEN
]
}
`
,
...(
csrfToken
?
{
'
x-csrf-token
'
:
csrfToken
}
:
{}),
};
};
const
url
=
new
URL
(
path
,
apiEndpoint
);
const
url
=
new
URL
(
path
,
apiEndpoint
);
...
...
lib/hooks/useFetch.tsx
View file @
091537b9
...
@@ -20,9 +20,13 @@ export default function useFetch() {
...
@@ -20,9 +20,13 @@ export default function useFetch() {
return
React
.
useCallback
(<
Success
,
Error
>
(path: string, params?: Params): Promise
<
Success
|
ResourceError
<
Error
>
>
=
>
{
return
React
.
useCallback
(<
Success
,
Error
>
(path: string, params?: Params): Promise
<
Success
|
ResourceError
<
Error
>
>
=
>
{
const
reqParams
=
{
const
reqParams
=
{
...
params
,
...
params
,
body
:
params
?.
method
&&
!
[
'
GET
'
,
'
HEAD
'
].
includes
(
params
.
method
)
?
body
:
params
?.
method
&&
params
?.
body
&&
!
[
'
GET
'
,
'
HEAD
'
].
includes
(
params
.
method
)
?
JSON
.
stringify
(
{
...
params
?.
body
,
_csrf_token
:
token
}
)
:
JSON
.
stringify
(
params
.
body
)
:
undefined
,
undefined
,
headers
:
{
...
params
?.
headers
,
...(
token
?
{
'
x-csrf-token
'
:
token
}
:
{}),
},
};
};
return
fetch
(
path
,
reqParams
).
then
(
response
=>
{
return
fetch
(
path
,
reqParams
).
then
(
response
=>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment