Skip to content

Commit

Permalink
exporting credentials and auth client from server
Browse files Browse the repository at this point in the history
  • Loading branch information
pan261 committed Nov 8, 2021
1 parent 2cecd17 commit f76f142
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
64 changes: 39 additions & 25 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ const ForgeSDK = require('forge-apis');
Navigate to http://localhost:3000/auth/ to get started
Browser should then go through the Autodesk Auth screen and display Auth token.
*/
const scopes = ['bucket:create', 'bucket:read', 'data:read', 'data:create', 'data:write'];

var oAuth2ThreeLegged = new ForgeSDK.AuthClientThreeLegged(
process.env.FORGE_CLIENT_ID,
process.env.FORGE_CLIENT_SECRET,
process.env.FORGE_CALLBACK_URL,
scopes,
true
);

var credentials;

/*
Function to start the server
Expand All @@ -30,63 +40,67 @@ const ForgeSDK = require('forge-apis');
function createServer() {
const app = express();
const PORT = process.env.PORT || 3000;
const scopes = ['bucket:create', 'bucket:read', 'data:read', 'data:create', 'data:write'];
const oAuth2ThreeLegged = new ForgeSDK.AuthClientThreeLegged(
process.env.FORGE_CLIENT_ID,
process.env.FORGE_CLIENT_SECRET,
process.env.FORGE_CALLBACK_URL,
scopes,
true
);

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;
console.error('Missing FORGE_CLIENT_ID or FORGE_CLIENT_SECRET env. variables.');
return;
}

app.use(express.static(path.join(__dirname, 'public')));
app.use(cookieSession({
name: 'forge_session',
keys: ['forge_secure_key'],
maxAge: 14 * 24 * 60 * 60 * 1000 // 14 days, same as refresh token
name: 'forge_session',
keys: ['forge_secure_key'],
maxAge: 14 * 24 * 60 * 60 * 1000 // 14 days, same as refresh token
}));

// Endpoint to begin authentication process
app.get('/auth', function (req, res) {
res.redirect(oAuth2ThreeLegged.generateAuthUrl());
res.redirect(oAuth2ThreeLegged.generateAuthUrl());
});

// Endpoint Forge redirects to after consent screen
app.get('/callback', function (req, res) {
oAuth2ThreeLegged.getToken(req.query.code).then(function (credentials) {
console.log(credentials)
res.send('Generated token: ' + credentials.access_token);
}, function(err){
oAuth2ThreeLegged.getToken(req.query.code).then((credentials) => {
setCredentials(credentials)
res.send("Generated token: " + credentials.access_token)
}, (err) => {
console.error(err);
res.send('Failed to authenticate');
});
});

app.use((err, req, res, next) => {
console.error(err);
res.status(err.statusCode).json(err);
console.error(err);
res.status(err.statusCode).json(err);
});

const server = app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); });

return server;
}

const setCredentials = (cr) => {
credentials = cr
console.log(cr)
}

const getCredentials = () => {
return credentials
}

/*
Function to shutdown the server
*/
function shutdownServer(server) {
server.close(() => {
console.log('Closing connections');
return;
console.log('Closing connections');
return;
});
}


module.exports.create = createServer;
module.exports.shutdown = shutdownServer;
module.exports = {
create: createServer,
shutdown: shutdownServer,
authClient: oAuth2ThreeLegged,
getCredentials: getCredentials
}
3 changes: 3 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const authServer = require('./server');

function intervalFunc() {
console.log('Running every 15 sec');
cr = authServer.getCredentials();
console.log(cr);
console.log(authServer.authClient);
}

(async () => {
Expand Down

0 comments on commit f76f142

Please sign in to comment.