Created
June 18, 2020 00:13
-
-
Save gazorby/a7b39b911cf92d116dcf113560088601 to your computer and use it in GitHub Desktop.
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
/** | |
* Set headers according to current project mode | |
* In development, we send JWT token via Authorization header | |
* while they are stored in httponly cookie in production | |
*/ | |
const authLink = setContext(async (_, { headers }) => { | |
// Use your async token function here: | |
const csrfToken = await getCsrfToken(); | |
// Return the headers to the context so httpLink can read them | |
if (process.env.NODE_ENV === "development") { | |
const jwtToken = localStorage.getItem("jwtToken"); | |
return { | |
headers: { | |
...headers, | |
"X-CSRFToken": csrfToken, | |
authorization: jwtToken ? `JWT ${jwtToken}` : "" | |
} | |
}; | |
} else { | |
return { | |
credentials: "include", | |
headers: { | |
...headers, | |
"X-CSRFToken": csrfToken | |
} | |
}; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment