From de316f5e97128fb28219744545f011c2ed1be892 Mon Sep 17 00:00:00 2001 From: pan261 Date: Tue, 2 Nov 2021 22:38:18 -0400 Subject: [PATCH] moved environment variable to .env --- config.js | 14 -------------- package.json | 4 ++-- puppeteer.js | 5 +++++ start.js => server.js | 26 ++++++++++++++++++-------- 4 files changed, 25 insertions(+), 24 deletions(-) delete mode 100644 config.js rename start.js => server.js (68%) diff --git a/config.js b/config.js deleted file mode 100644 index d8958b8..0000000 --- a/config.js +++ /dev/null @@ -1,14 +0,0 @@ -module.exports = { - // Set environment variables or hard-code here - credentials: { - client_id: process.env.FORGE_CLIENT_ID, - client_secret: process.env.FORGE_CLIENT_SECRET, - callback_url: process.env.FORGE_CALLBACK_URL - }, - scopes: { - // Required scopes for the server-side application - internal: ['bucket:create', 'bucket:read', 'data:read', 'data:create', 'data:write'], - // Required scope for the client-side viewer - public: ['viewables:read'] - } -}; \ No newline at end of file diff --git a/package.json b/package.json index 43f01f9..f4a1d4f 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,9 @@ "name": "fusionfilesync", "description": "", "version": "1.0.0", - "main": "start.js", + "main": "server.js", "scripts": { - "start": "node start.js", + "start": "node server.js", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { diff --git a/puppeteer.js b/puppeteer.js index 2629de7..fd5f76a 100644 --- a/puppeteer.js +++ b/puppeteer.js @@ -18,6 +18,11 @@ require('dotenv').config(); */ (async () => { + if (process.env.AUTODESK_EMAIL == null || process.env.AUTODESK_PASSWORD == null) { + console.error('Missing AUTODESK_EMAIL or AUTODESK_PASSWORD env. variables.'); + return; + } + const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox'] diff --git a/start.js b/server.js similarity index 68% rename from start.js rename to server.js index 5b2a0e1..ebb7a12 100644 --- a/start.js +++ b/server.js @@ -7,7 +7,6 @@ const bodyParser = require('body-parser'); require('dotenv').config(); const PORT = process.env.PORT || 3000; -const config = require('./config'); /* Resources used: @@ -15,13 +14,24 @@ const config = require('./config'); https://learnforge.autodesk.io/#/oauth/3legged/nodejs https://www.npmjs.com/package/forge-apis https://forge.autodesk.com/blog/3legged-token-process-nodejs-cli + + Server running on port 3000 used to get 3-legged access token. + Must have FORGE_CLIENT_ID, FORGE_CLIENT_SECRET, and FORGE_CALLBACK_URL set in .env file. + FORGE_CALLBACK_URL (should be 'http://localhost:3000/callback/') should be the same in the .env + and in the Forge API project console. + + Usage: node server.js or npm start + Navigate to http://localhost:3000/auth/ to get started + Browser should then go through the Autodesk Auth screen and display Auth token. */ -if (config.credentials.client_id == null || config.credentials.client_secret == null) { +if (process.env.FORGE_CLIENT_ID == null || process.env.FORGE_CLIENT_SECRET == null) { console.error('Missing FORGE_CLIENT_ID or FORGE_CLIENT_SECRET env. variables.'); return; } +const scopes = ['bucket:create', 'bucket:read', 'data:read', 'data:create', 'data:write']; + let app = express(); app.use(express.static(path.join(__dirname, 'public'))); app.use(cookieSession({ @@ -33,9 +43,9 @@ app.use(cookieSession({ app.get('/auth', function (req, res) { var redirect_uri = 'https://developer.api.autodesk.com/authentication/v1/authorize?' + 'response_type=code' - + '&client_id=' + config.credentials.client_id - + '&redirect_uri=' + config.credentials.callback_url - + '&scope=' + config.scopes.internal.join(' '); + + '&client_id=' + process.env.FORGE_CLIENT_ID + + '&redirect_uri=' + process.env.FORGE_CALLBACK_URL + + '&scope=' + scopes.join(' '); console.log(redirect_uri); res.redirect(redirect_uri); }); @@ -48,11 +58,11 @@ app.get('/callback', function (req, res) { 'content-type': 'application/x-www-form-urlencoded', }, data: querystring.stringify({ - client_id: config.credentials.client_id, - client_secret: config.credentials.client_secret, + client_id: process.env.FORGE_CLIENT_ID, + client_secret: process.env.FORGE_CLIENT_SECRET, grant_type: 'authorization_code', code: req.query.code, - redirect_uri: config.credentials.callback_url + redirect_uri: process.env.FORGE_CALLBACK_URL }) }) .then(function (response) {