IO-256 QBO Authorization Flow.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import axios from "axios";
|
||||
import { auth } from "../firebase/firebase.utils";
|
||||
|
||||
import { Cookies } from "react-cookie";
|
||||
|
||||
const cookies = new Cookies();
|
||||
if (process.env.NODE_ENV === "production") {
|
||||
axios.defaults.baseURL =
|
||||
process.env.REACT_APP_AXIOS_BASE_API_URL || "https://api.imex.online/";
|
||||
@@ -14,6 +17,39 @@ export const axiosAuthInterceptorId = axios.interceptors.request.use(
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
}
|
||||
|
||||
//Check if Qbo Cookies need to be refreshed.
|
||||
const qbo_access_token = cookies.get("qbo_access_token");
|
||||
const qbo_refresh_token = cookies.get("qbo_refresh_token");
|
||||
|
||||
if (!qbo_refresh_token) {
|
||||
//Kill both values just in case.
|
||||
cookies.remove("qbo_access_token");
|
||||
cookies.remove("qbo_refresh_token");
|
||||
return config;
|
||||
}
|
||||
//Are they expired?
|
||||
if (!qbo_access_token) {
|
||||
//Refresh it first.
|
||||
const response = await axios.get(`/qbo/refresh`, {
|
||||
withCredentials: true,
|
||||
});
|
||||
|
||||
let expires = new Date();
|
||||
expires.setTime(expires.getTime() + response.data.expires_in * 1000);
|
||||
cookies.set("qbo_access_token", response.data.access_token, {
|
||||
path: "/",
|
||||
expires,
|
||||
});
|
||||
expires = new Date();
|
||||
expires.setTime(
|
||||
expires.getTime() + response.data.x_refresh_token_expires_in * 1000
|
||||
);
|
||||
cookies.set("qbo_refresh_token", response.data.refresh_token, {
|
||||
path: "/",
|
||||
expires,
|
||||
});
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error) => Promise.reject(error)
|
||||
|
||||
Reference in New Issue
Block a user