Skip to content

Instantly share code, notes, and snippets.

@gazorby
Created June 18, 2020 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gazorby/a7b39b911cf92d116dcf113560088601 to your computer and use it in GitHub Desktop.
Save gazorby/a7b39b911cf92d116dcf113560088601 to your computer and use it in GitHub Desktop.
/**
* 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