Skip to content

[graphiql] Add cookie-to-header token CSRF support #3355

Closed Answered by acao
Kaemmelot asked this question in Q&A / Support
Discussion options

You must be logged in to vote

I haven't worked with angular in a while, so I will just give you a vanilla example. this also assumes you aren't processing react JSX and are following something like the CDN example. this also assumes the cookie is already set when accessing the graphiql instance. We've used this approach at several companies that used CSRF tokens and/or JWT with cookies

const headers = ''
if (typeof window !== undefined) {
// maybe angular has a utility for accessing cookies?
 const getCookie = name => document.cookie.match(new RegExp(`(^| )${name}=([^;]+)`))?.at(2);
 const value = getCookie('CSRF COOKIE NAME')
 headers = `{ "X-CSRF-TOKEN" : "${value}" }`
}

 const root = ReactDOM.createRoot(document.g…

Replies: 5 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by acao
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
Converted from issue

This discussion was converted from issue #3354 on July 18, 2023 19:44.