NicholasByDesign
NicholasByDesign

Reputation: 801

Meteor: Get query parameters from url on server? Use case Instagram oAuth

This is easy to do with a client side router or JS on the client. But since window is not an object on the server how would one get a query parameter or even read a url from the server? I checked node examples but couldn't find anything that didn't pertain to express js.

My use case is Instagram, It sends me back a code that needs to be read on the server and then I send a http request with that code from the server to retrieve an access token.

Has to be on page load, not load then send to server via client.

Basically I want to retrieve the query of "code" http://localhost:3000/?code=5e04c2e304f24f8b8380c2ec81202139 on the server.

Upvotes: 0

Views: 451

Answers (1)

kkkkkkk
kkkkkkk

Reputation: 7748

I read the Instagram instruction for authentication, it seems that you are using the Server-side flow. I do not know why you choose this over the Client-side authentication flow which seems to be more appropriate, I think you should re-consider which method to use. Anyway, to answer your question you could use WebApp package to define a server route in Meteor:

WebApp.connectHandlers.use("/", function(req, res, next) {
  if (req._parsedUrl.pathname === '/') {
    const code = req.query.code;
    console.log(code);

    // get ACCESS_TOKEN with code

    // if you need to redirect user after authorization
    // res.writeHead(302, {
    //   Location: '/route/after/authorization'
    // });
    // res.end();
  }

  // comment this if you redirect to another route
  next();
});

Upvotes: 1

Related Questions