Skip to content

Commit

Permalink
moved environment variable to .env
Browse files Browse the repository at this point in the history
  • Loading branch information
pan261 committed Nov 3, 2021
1 parent b0683a7 commit de316f5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
14 changes: 0 additions & 14 deletions config.js

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
5 changes: 5 additions & 0 deletions puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
26 changes: 18 additions & 8 deletions start.js → server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,31 @@ const bodyParser = require('body-parser');
require('dotenv').config();

const PORT = process.env.PORT || 3000;
const config = require('./config');

/*
Resources used:
https://forge.autodesk.com/developer/learn/threelegged-auth/understand-code
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({
Expand All @@ -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);
});
Expand All @@ -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) {
Expand Down

1 comment on commit de316f5

@pan261
Copy link
Contributor Author

@pan261 pan261 commented on de316f5 Nov 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.env file should look like this:

AUTODESK_EMAIL=[email]
AUTODESK_PASSWORD=[password]
FORGE_CLIENT_ID=[client_id]
FORGE_CLIENT_SECRET=[client_secret]
FORGE_CALLBACK_URL=http://localhost:3000/callback/

Please sign in to comment.