Skip to content

Commit

Permalink
First version of Harvest Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
BramEsposito committed Jan 27, 2020
1 parent 8145620 commit 63240a3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
.ideo
node_modules
Empty file added examples/existing-express.js
Empty file.
51 changes: 51 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Authenticate on the v2 version of the Harvest API
*/
const express = require('express');
const session = require('express-session');
const grant = require('grant-express');
const request = require('request');

module.exports = function(data,callback) {
const app = express();

app.use(session({secret: data.APP_SECRET, saveUninitialized: true, resave: true}));
app.use(grant({
defaults: {
protocol: data.PROTOCOL,
host: data.HOST
},
harvestv2: {
"authorize_url": "https://id.getharvest.com/oauth2/authorize",
"access_url": "https://id.getharvest.com/api/v2/oauth2/token",
"oauth": 2,
key: data.CLIENT_ID,
secret: data.CLIENT_SECRET,
"callback": "/auth/harvestv2"
}
}));

app.get('/auth/harvestv2', (req, res) => {

const options = {
url: 'https://id.getharvest.com/api/v2/accounts',
headers: {
'Authorization': 'Bearer ' + req.query.access_token,
'User-Agent': data.APP_NAME
},
json:true
};

request(options, (qerr, qres, qbody) => {

if (qerr) {
res.send("Error requsting harvest accounts: "+qerr);
return console.log(qerr);
}

callback(res, {tokens: req.query, accounts: qbody});
});
});

return app;
};
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@
"bugs": {
"url": "https://github.com/BramEsposito/node-harvest-auth/issues"
},
"homepage": "https://github.com/BramEsposito/node-harvest-auth#readme"
"homepage": "https://github.com/BramEsposito/node-harvest-auth#readme",
"dependencies": {
"express-session": "^1.17.0",
"grant-express": "^4.7.0",
"request": "^2.88.0"
}
}
12 changes: 11 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
# Authenticate via the Harvest API
# Authenticate via the Harvest API

## Requirements

You need [Express](https://expressjs.com/) to use this module

## How to use

Install using npm: "npm i harvest-auth"

Take a look at at the [example implementation]("examples/existing-express.js")

0 comments on commit 63240a3

Please sign in to comment.