-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
014fcc6
commit 3979fe0
Showing
4 changed files
with
39 additions
and
52 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,43 @@ | ||
import axios from "axios"; | ||
import { API_BASE_URL } from "./config"; | ||
import { API_BASE_URL, ACTIVITY_LOGS_API_URL } from "./config"; | ||
|
||
// Create activity axios instance | ||
const userActivityLoggerAxios = axios.create({ | ||
baseURL: ACTIVITY_LOGS_API_URL, | ||
}); | ||
|
||
const instance = axios.create({ | ||
baseURL: API_BASE_URL, | ||
}); | ||
|
||
instance.defaults.headers.Authorization = `Bearer ${localStorage.getItem( | ||
"token" | ||
)}`; | ||
|
||
instance.interceptors.response.use( | ||
(response) => response, | ||
(error) => { | ||
let e = error; | ||
// this checks for when a token is not verified and logs you out | ||
if (error.response.status === 401 || error.response.status === 422) { | ||
localStorage.clear(); | ||
window.location.href = "/"; | ||
} else if(error.response.status === 502){ | ||
e = "Your request too long possible Server Error." | ||
window.location.href = "/projects"; | ||
} | ||
return Promise.reject(e); | ||
// Set default headers | ||
const token = localStorage.getItem("token"); | ||
instance.defaults.headers.Authorization = `Bearer ${token}`; | ||
userActivityLoggerAxios.defaults.headers.Authorization = `Bearer ${token}`; | ||
|
||
// Define response interceptor function | ||
const responseInterceptor = (response) => response; | ||
|
||
const errorInterceptor = (error) => { | ||
let e = error; | ||
// this checks for when a token is not verified and logs you out | ||
if (error.response.status === 401 || error.response.status === 422) { | ||
localStorage.clear(); | ||
window.location.href = "/"; | ||
} else if (error.response.status === 502) { | ||
e = "Your request took too long, possibly a Server Error."; | ||
window.location.href = "/projects"; | ||
} | ||
return Promise.reject(e); | ||
}; | ||
|
||
// Add response interceptors to both instances | ||
instance.interceptors.response.use(responseInterceptor, errorInterceptor); | ||
|
||
userActivityLoggerAxios.interceptors.response.use( | ||
responseInterceptor, | ||
errorInterceptor | ||
); | ||
|
||
export default instance; | ||
export { instance, userActivityLoggerAxios }; | ||
export default instance; |
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