IO-256 Authorization and Basic Calls
This commit is contained in:
@@ -5,12 +5,14 @@ require("dotenv").config({
|
||||
`.env.${process.env.NODE_ENV || "development"}`
|
||||
),
|
||||
});
|
||||
const logger = require("../../utils/logger");
|
||||
const OAuthClient = require("intuit-oauth");
|
||||
|
||||
var QuickBooks = require("node-quickbooks");
|
||||
const Promise = require("bluebird");
|
||||
const QuickBooksPromise = Promise.promisifyAll(QuickBooks.prototype);
|
||||
|
||||
const client = require("../../graphql-client/graphql-client").client;
|
||||
const queries = require("../../graphql-client/queries");
|
||||
const queryString = require("query-string");
|
||||
const oauthClient = new OAuthClient({
|
||||
clientId: process.env.QBO_CLIENT_ID,
|
||||
clientSecret: process.env.QBO_SECRET,
|
||||
@@ -20,50 +22,62 @@ const oauthClient = new OAuthClient({
|
||||
});
|
||||
|
||||
exports.default = async (req, res) => {
|
||||
// Parse the redirect URL for authCode and exchange them for tokens
|
||||
|
||||
const params = queryString.parse(req.url.split("?").reverse()[0]);
|
||||
try {
|
||||
// Exchange the auth code retrieved from the **req.url** on the redirectUri
|
||||
logger.log("qbo-callback-create-token", "DEBUG", params.state, null, null);
|
||||
const authResponse = await oauthClient.createToken(req.url);
|
||||
if (authResponse.json.error) {
|
||||
logger.log("qbo-callback-error", "ERROR", params.state, null, {
|
||||
error: authResponse.json,
|
||||
});
|
||||
res.redirect(
|
||||
`http://localhost:3000/manage/accounting/qbo?error=${encodeURIComponent(
|
||||
JSON.stringify(authResponse.json)
|
||||
)}`
|
||||
);
|
||||
} else {
|
||||
await client.request(queries.SET_QBO_AUTH, {
|
||||
email: params.state,
|
||||
qbo_auth: { ...authResponse.json, createdAt: Date.now() },
|
||||
});
|
||||
logger.log(
|
||||
"qbo-callback-create-token-success",
|
||||
"DEBUG",
|
||||
params.state,
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
const { access_token, refresh_token } = authResponse.json;
|
||||
|
||||
//store this information against the assocation record.
|
||||
|
||||
//Send a redirect back to the imex online application
|
||||
res.json(authResponse.json);
|
||||
|
||||
// var qbo = new QuickBooks(
|
||||
// process.env.QBO_CLIENT_ID,
|
||||
// process.env.QBO_SECRET,
|
||||
// access_token,
|
||||
// false, // no token secret for oAuth 2.0
|
||||
// realmId,
|
||||
// process.env.NODE_ENV !== "production", // use the sandbox?, // use the sandbox?
|
||||
// true, // enable debugging?
|
||||
// null, // set minorversion, or null for the latest version
|
||||
// "2.0", //oAuth version
|
||||
// refresh_token
|
||||
// );
|
||||
|
||||
// qbo.findInvoices({ fetchAll: true }, (errors, invoices) =>
|
||||
// console.log(errors, invoices)
|
||||
// );
|
||||
res.redirect(`http://localhost:3000/manage/accounting/qbo?`);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("The error message is :" + JSON.stringify(e, null, 2));
|
||||
console.error(e.intuit_tid);
|
||||
res.status(500).json(e);
|
||||
logger.log("qbo-callback-error", "ERROR", params.state, null, {
|
||||
error: e,
|
||||
});
|
||||
res.status(400).json(e);
|
||||
}
|
||||
};
|
||||
|
||||
exports.refresh = async (req, res) => {
|
||||
exports.refresh = async (oauthClient, req) => {
|
||||
try {
|
||||
oauthClient.setToken(req.cookies.qbo_access_token);
|
||||
const authResponse = oauthClient.refreshUsingToken(
|
||||
req.cookies.qbo_refresh_token
|
||||
);
|
||||
res.json(authResponse.json);
|
||||
logger.log("qbo-token-refresh", "DEBUG", req.user.email, null, null);
|
||||
const authResponse = await oauthClient.refresh();
|
||||
await client.request(queries.SET_QBO_AUTH, {
|
||||
email: req.user.email,
|
||||
qbo_auth: { ...authResponse.json, createdAt: Date.now() },
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json(error);
|
||||
logger.log("qbo-token-refresh-error", "ERROR", req.user.email, null, {
|
||||
error,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
exports.setNewRefreshToken = async (email, apiResponse) => {
|
||||
logger.log("qbo-token-updated", "DEBUG", email, null, null);
|
||||
|
||||
await client.request(queries.SET_QBO_AUTH, {
|
||||
email,
|
||||
qbo_auth: { ...apiResponse.token, createdAt: Date.now() },
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user