Last active
May 2, 2020 04:30
-
-
Save crrmacarse/bae6133bff7d4d865610978d03033279 to your computer and use it in GitHub Desktop.
Axios config + CSRF Token
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
/** 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