-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from piny940/csrf
Csrf対策を追加
- Loading branch information
Showing
4 changed files
with
29 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
import createClient from 'openapi-fetch' | ||
import { paths } from './api' | ||
import { fromCookie } from './cookie' | ||
|
||
export const client = createClient<paths>({ | ||
fetch: (input: Request) => { | ||
const csrf = fromCookie('_csrf') | ||
if (csrf) { | ||
input.headers.set('X-CSRF-Token', csrf) | ||
} | ||
return fetch(input) | ||
}, | ||
baseUrl: process.env.NEXT_PUBLIC_API_URL, | ||
credentials: 'include', | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const fromCookie = (key: string): string | undefined => { | ||
const match = document.cookie.match(new RegExp('(^| )' + key + '=([^;]+)')) | ||
if (match) return match[2] | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters