bartve / disconnect

An easy to use Node.js client with OAuth support to connect with the discogs.com API v2.0
MIT License
458 stars 79 forks source link

requestData is out of scope in get("/callback") request #45

Closed plamalfa closed 6 years ago

plamalfa commented 6 years ago

Had to define requestData as a global variable to use in the get request for the callback URL.


let _requestData;
app.get("/authorize", function(req, res) {
  var oAuth = new Discogs().oauth();
  oAuth.getRequestToken(
    "sdfasdfadslfkjasdhf",
    "asdlfkjahsdflkajsdhf",
    "http://localhost:3000/callback",
    function(err, requestData) {
      _requestData = requestData;
      // Persist "requestData" here so that the callback handler can
      // access it later after returning from the authorize url
      res.redirect(requestData.authorizeUrl);
    }
  );
});

app.get("/callback", function(req, res) {
  var oAuth = new Discogs(_requestData).oauth();
  oAuth.getAccessToken(
    req.query.oauth_verifier, // Verification code sent back by Discogs
    function(err, accessData) {
      // Persist "accessData" here for following OAuth calls
      res.send("Received access token!");
    }
  );
});

Is the code above how you had intended the package to be used? A more complete example of these get requests in action would be helpful to understand their uses.

bartve commented 6 years ago

The global variable is your implementation of my comments

// Persist "requestData" here so that the callback handler can
// access it later after returning from the authorize url

I could give a literal example, but I just want to indicate one should store it somewhere in a scope so that it can be accessed by another request handler.

I could change it to something like this:

// Persist "requestData" here (in a global variable for example) so that the 
// callback handler can access it later after returning from the authorize url