Files
mwitnessing/src/axiosSecure.js
2024-02-22 04:19:38 +02:00

43 lines
1.3 KiB
JavaScript

import axios from "axios";
import common from "../src/helpers/common";
import { applyAuthTokenInterceptor } from 'axios-jwt';
const axiosInstance = axios.create({
baseURL: common.getBaseUrl(),
withCredentials: true,
// headers: {
// "Content-Type": "application/json",
// },
});
// 2. Define token refresh function.
const requestRefresh = (refresh) => {
// Notice that this is the global axios instance, not the axiosInstance! <-- important
return axios.post(`${common.getBaseUrl()}/auth/refresh_token`, { refresh })
.then(response => response.data.access_token)
};
// 3. Apply interceptor
applyAuthTokenInterceptor(axiosInstance, { requestRefresh }); // Notice that this uses the axiosInstance instance. <-- important
// 4. Logging in
const login = async (params) => {
const response = await axiosInstance.post('/api/auth/signin', params)
// save tokens to storage
setAuthTokens({
accessToken: response.data.access_token,
refreshToken: response.data.refresh_token
})
}
// 5. Logging out
const logout = () => clearAuthTokens()
// Now just make all requests using your axiosInstance instance
// axiosInstance.get('/api/data/locations').then(response => {
// console.log(response.data) // <-- this will be the response from your API
// })
export default axiosInstance;