IO-256 Authorization and Basic Calls

This commit is contained in:
Patrick Fic
2021-08-27 15:42:32 -07:00
parent 724c097d52
commit 5284ee2ef9
13 changed files with 6559 additions and 126 deletions

View File

@@ -9,34 +9,18 @@ import { useCookies } from "react-cookie";
export default function QboAuthorizeComponent() {
const location = useLocation();
const [cookies, setCookie] = useCookies(["access_token", "refresh_token"]);
const [, setCookie] = useCookies(["access_token", "refresh_token"]);
const handleQbSignIn = async () => {
const result = await Axios.post("/qbo/authorize");
console.log("pushing to history", result.data);
window.location.href = result.data;
};
const qs = queryString.parse(location.search);
const { error } = qs;
useEffect(() => {
const ExchangeForAccessToken = async () => {
const response = await Axios.get(`/qbo/callback${location.search}`);
let expires = new Date();
expires.setTime(expires.getTime() + response.data.expires_in * 1000);
setCookie("qbo_access_token", response.data.access_token, {
path: "/",
expires,
});
expires = new Date();
expires.setTime(
expires.getTime() + response.data.x_refresh_token_expires_in * 1000
);
setCookie("qbo_refresh_token", response.data.refresh_token, {
path: "/",
expires,
});
};
const qs = queryString.parse(location.search);
const { code, state, realmId } = qs;
const hasBeenCalledBack = code && realmId && state;
@@ -51,19 +35,19 @@ export default function QboAuthorizeComponent() {
path: "/",
expires,
});
ExchangeForAccessToken();
}
}, [location, setCookie]);
}, [qs, location, setCookie]);
return (
<div>
<Space>
<Space wrap>
<Button onClick={handleQbSignIn}>
<img
{/* <img
src={QboImg}
alt="Sign in with Intuit"
onClick={handleQbSignIn}
/>
/> */}
auth
</Button>
<Button
onClick={async () => {
@@ -75,7 +59,18 @@ export default function QboAuthorizeComponent() {
>
Refresh Token
</Button>
<Button
onClick={async () => {
const response = await Axios.post(`/qbo/receivables`, {
withCredentials: true,
});
console.log(response);
}}
>
REC
</Button>
</Space>
{error && JSON.parse(decodeURIComponent(error)).error_description}
</div>
);
}

View File

@@ -1,9 +1,6 @@
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/";
@@ -18,38 +15,6 @@ export const axiosAuthInterceptorId = axios.interceptors.request.use(
}
}
//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)