A
A
Alisa942019-01-31 10:40:29
JavaScript
Alisa94, 2019-01-31 10:40:29

How to pass promise value from one file to another?

There is a server file and an authentication file, I need to pass the received value from the authentication file to the server file. I use axios and primis technology. I confess: I understand almost nothing about promises and asynchrony, but I need quick help.
Below are these files.
server.js

const path = require('path');
const express = require('express');       
const Axios = require('axios');             
const bodyParser = require('body-parser');   
const querystring = require('querystring');

let app = express();
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));

const config = require('./config');

const PORT = config.credentials.PORT; 
var FORGE_CLIENT_ID = 'myID';
var FORGE_CLIENT_SECRET = 'mySecret';
const scopes = 'data:read data:write data:create bucket:create bucket:read';


app.use('/api/forge', require('./routes/oauth'));
app.use((err, req, res, next) => {
  console.error(err);
  res.status(err.statusCode).json(err);
});


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

oauth.js
const path = require('path');
const express = require('express');
const Axios = require('axios');
const bodyParser = require('body-parser');
const querystring = require('querystring');        

const config = require('../config');

var FORGE_CLIENT_ID = 'myId';
var FORGE_CLIENT_SECRET = 'mySecret';
const scopes = 'data:read data:write data:create bucket:create bucket:read';

let router = express.Router();
router.use(bodyParser.json());

let access_token = '';

router.get('/oauth', function (req, res) {
  Axios({
    method: 'POST',
    url: 'https://developer.api.autodesk.com/authentication/v1/authenticate',
    headers: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    data: querystring.stringify({
      client_id: FORGE_CLIENT_ID,
      client_secret: FORGE_CLIENT_SECRET,
      grant_type: 'client_credentials',
      scope: scopes
    })
  })
  .then(function (response) {
    access_token = response.data.access_token;
    console.log(response);
    res.redirect('/api/forge/datamanagement/bucket/create');
  })
  .catch(function (error) {
    // Failed
    console.error(error);
    res.send('Failed to authenticate');
  });
});

module.exports = router;

In order to proceed with the authentication, I have to get the access_token variable from the oauth.js file in the server.js file. I tried all the ways (that I knew), but it does not work. I would be very grateful for any help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Abcdefgk, 2019-01-31
@Alisa94

The principle is this.
You write an oauth.js module that looks like this:

const Axios = require('axios');             
const querystring = require('querystring');

var FORGE_CLIENT_ID = 'myId';
var FORGE_CLIENT_SECRET = 'mySecret';
const scopes = 'data:read data:write data:create bucket:create bucket:read';

module.exports = function() {
  return Axios({
    method: 'POST',
    url: 'https://developer.api.autodesk.com/authentication/v1/authenticate',
    headers: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    data: querystring.stringify({
      client_id: FORGE_CLIENT_ID,
      client_secret: FORGE_CLIENT_SECRET,
      grant_type: 'client_credentials',
      scope: scopes
    })
  });
};

And in the place where it is needed, you call it with a continuation
require('./oauth')
  .then(function (response) {
    access_token = response.data.access_token;
  })
  .catch(...

The principle itself. And what should be the continuation, where, some kind of redirect to then - where? This is something you don't understand. Here the problem is in the most general construction (and not in the syntax-schmintaxis, of course).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question