diff --git a/.gitignore b/.gitignore index 61630d3..3c3629e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -.ideo node_modules diff --git a/examples/existing-express.js b/examples/existing-express.js new file mode 100644 index 0000000..e69de29 diff --git a/index.js b/index.js new file mode 100644 index 0000000..ee7a757 --- /dev/null +++ b/index.js @@ -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; +}; diff --git a/package.json b/package.json index 81413b2..64ed78a 100644 --- a/package.json +++ b/package.json @@ -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" + } } diff --git a/readme.md b/readme.md index 6eaeae8..d9f919d 100644 --- a/readme.md +++ b/readme.md @@ -1 +1,11 @@ -# Authenticate via the Harvest API \ No newline at end of file +# 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")