Skip to content

Commit cd7e9b1

Browse files
authored
fix: use latest CSRF tokens from window for all requests (#66)
1 parent 01c34c0 commit cd7e9b1

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/utils/axios.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,28 @@ export function getAxiosClient(
77
tokenType?: 'Bearer' | 'token',
88
customHeaders?: object
99
): AxiosInstance {
10-
return axios.create({
10+
const axiosInstance = axios.create({
1111
baseURL: appURL,
1212
headers: getRequestHeaders(useToken, tokenType, token, appURL, customHeaders),
1313
withCredentials: true,
1414
});
15+
16+
// Add request interceptor to dynamically set CSRF token and auth token
17+
axiosInstance.interceptors.request.use((config) => {
18+
// Update CSRF token on each request if available
19+
if (typeof window !== 'undefined' && window.csrf_token && window.csrf_token !== '{{ csrf_token }}') {
20+
config.headers['X-Frappe-CSRF-Token'] = window.csrf_token;
21+
}
22+
23+
// Update authorization token if using token auth
24+
if (useToken && tokenType && token) {
25+
config.headers.Authorization = `${tokenType} ${token()}`;
26+
}
27+
28+
return config;
29+
});
30+
31+
return axiosInstance;
1532
}
1633

1734
export function getRequestHeaders(

0 commit comments

Comments
 (0)