initial commit - code moved to separate repo
This commit is contained in:
42
src/axiosSecure.js
Normal file
42
src/axiosSecure.js
Normal file
@ -0,0 +1,42 @@
|
||||
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;
|
Reference in New Issue
Block a user