This template (or boiler plate) implements a basic infrastrucutre for Autodesk Forge 3 Legged OAuth with NodeJS. The front-end will look like:
For using this sample, you need an Autodesk developer credentials. Visit the Forge Developer Portal, sign up for an account, then create an app. For this new app, use http://localhost:3000/api/forge/callback/oauth as Callback URL. Finally take note of the Client ID and Client Secret.
Install NodeJS.
Clone this project or download it. It's recommended to install GitHub desktop. To clone it via command line, use the following (Terminal on MacOSX/Linux, Git Shell on Windows):
git clone https://github.com/augustogoncalves/forge-3leg.nodejs-template
To run it, install the required packages, set the enviroment variables with your client ID & secret and finally start it. Via command line, navigate to the folder where this repository was cloned and use the following:
Mac OSX/Linux (Terminal)
npm install
export FORGE_CLIENT_ID=<<YOUR CLIENT ID FROM DEVELOPER PORTAL>>
export FORGE_CLIENT_SECRET=<<YOUR CLIENT SECRET>>
npm run dev
Windows (use Node.js command line from Start menu)
npm install
set FORGE_CLIENT_ID=<<YOUR CLIENT ID FROM DEVELOPER PORTAL>>
set FORGE_CLIENT_SECRET=<<YOUR CLIENT SECRET>>
npm run dev
Open the browser: http://localhost:3000.
Important: do not use npm start locally, this is intended for PRODUCTION only with HTTPS (SSL) secure cookies.
To deploy this application to Heroku, the Callback URL must use your .herokuapp.com address. After clicking on the button below, at the Heroku Create New App page, set your Client ID & Secret and the correct callback URL.
Watch this video on how deploy this sample to Heroku.
Your application endpoints should be implemented at /server/app.js. Use the Token module to read the Public and Internal tokens. Only send Public token to the front-end, use the Internal token to perform server-side operations with Autodesk APIs.
Below is a small section of the app.js file. Adjust the get method and replace your_end_point endpoint with your own.
// token handling in session
var token = require('./token');
router.get('/your_end_point', function (req, res) {
var tokenSession = new token(req.session);
var tokenInternal = tokenSession.getTokenInternal();
// ToDo: implement your application here, e.g. call Autodesk endpoints
});
When your application is live, consider changing the secure attribute on /server/server.js to true
All Autodesk Forge NPM packages are included by default, see complete list of what's available at NPM website. Some other non-Autodesk packaged are used, including express and its session/cookie middlewares (express-session and cookie-parser) for user session handling. The front-end uses bootstrap and jquery.
For local development/testing, consider use nodemon package, which auto restart your node application after any modification on your code. To install it, use:
sudo npm install -g nodemon
Then, instead of npm run dev, use the following:
npm run nodemon
Which executes nodemon server.js --ignore www/, where the --ignore parameter indicates that the app should not restart if files under www folder are modified.
After installing Github desktop for Windows, on the Git Shell, if you see a error setting certificate verify locations error, use the following:
git config --global http.sslverify "false"
This sample is licensed under the terms of the MIT License. Please see the LICENSE file for full details.
Augusto Goncalves (Forge Partner Development)
http://forge.autodesk.com