Friday, 2 June 2017

how to pass parameter to '/callback' middleware in spotify web api

I was playing around with Spotify's Web API tutorial and I was changing the example code they put on github.

here is the snippet I had a problem with:

    app.get('/login', function(req, res) {

      var state = generateRandomString(16);   res.cookie(stateKey, state);

      // your application requests authorization   var scope = 'user-read-private user-read-email';   res.redirect('https://accounts.spotify.com/authorize?' +
        querystring.stringify({
          response_type: 'code',
          client_id: client_id,
          scope: scope,
          redirect_uri: redirect_uri,
          state: state
        })); });

    app.get('/callback', function(req, res) {

      // your application requests refresh and access tokens   // after checking the state parameter

   var code = req.query.code || null;
   var state = req.query.state || null;
   var storedState = req.cookies ? req.cookies[stateKey] : null;
      .
      .
      .

I wanted to pass a userId as parameter to '/callback' so I simply edited the '/login' to '/login/:userId' and then I got the userId inside of the '/login''s scope successfully by using req.params.userId. However, I tried many ways to pass this on to the callback (which the login will redirect to) but I couldn't. For example, I tried simply adding it to the queryString but it was always undefined.

What is the best way to pass a parameter to this /callback route, given that we redirect to it after /login?

Help would be much appreciated :)



via Amer Mograbi

No comments:

Post a Comment