Skip to content

Instantly share code, notes, and snippets.

@crrmacarse
Last active May 2, 2020 04:30
Show Gist options
  • Save crrmacarse/bae6133bff7d4d865610978d03033279 to your computer and use it in GitHub Desktop.
Save crrmacarse/bae6133bff7d4d865610978d03033279 to your computer and use it in GitHub Desktop.
Axios config + CSRF Token
/** Retrieve token cookie generated by backend */
const [cookieCsrfToken] = document.cookie.split('; ').filter((v) => /^xsrf-token=/.test(v));
const csrfToken = cookieCsrfToken.replace('xsrf-token=', '');
const DEFAULT_API_BASE_URL = `${process.env.API_BASE_URL}/api`;
const API_BASE_URL = process.env.NODE_ENV === 'development' && process.env.API_BASE_URL
? DEFAULT_API_BASE_URL
: '/api';
/**
* Creates a base axios instance
*/
const api = axios.create({
baseURL: API_BASE_URL,
responseType: 'json',
xsrfHeaderName: 'X-XSRF-TOKEN',
headers: {
'X-XSRF-TOKEN': csrfToken,
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment