GithubHelp home page GithubHelp logo

Comments (3)

Dashron avatar Dashron commented on May 29, 2024

It depends on your use case.

If all of your Vimeo API requests will be authenticated as a single user (such as if you are building a website that only makes API calls to retrieve https://vimeo.com/dashron's videos) then you can create the library outside of the routes and assign the access token there too.

If your Vimeo API requests will be authenticated by different users depending on who is using your website, you should construct a new Vimeo object within each route, such as below:

// renders a link that redirects the user to vimeo auth
app.get('/', (req, res, next) => {
  const vimeo = new Vimeo(VIMEO_CLIENT_ID, VIMEO_CLIENT_SECRET);
  const vimeoState = 1; // TODO: change to generate random string
  const loginUrl = vimeo.buildAuthorizationEndpoint(VIMEO_REDIRECT_URL, VIMEO_SCOPES, vimeoState);
  res.render('index', {loginUrl});
});

// handle response
app.get('/auth', (req, res, next) => {
  const vimeo = new Vimeo(VIMEO_CLIENT_ID, VIMEO_CLIENT_SECRET);
  const code = req.query.code;
  const state = req.query.state;

  vimeo.accessToken(code, VIMEO_REDIRECT_URL, (err, token) => {
    if (err) { return next(err); }

    if (token.access_token) {
      // Note: what to do if multiple users connected?
      vimeo.access_token = token.access_token;
      req.session.vimeo = {token};
    }
  });
});

from vimeo.js.

renarsvilnis avatar renarsvilnis commented on May 29, 2024

Thanks, just as I thought.
Also note for anyone reading it: Good idea to wrap the Vimeo instance creation in an factory-function. So you won't need to reference the client_id, ... all the time.

from vimeo.js.

Dashron avatar Dashron commented on May 29, 2024

Totally agree about the factory function. In my personal work I tend to have something like...

/src/lib/Vimeo.js

const config = require('config');
const Vimeo = require('vimeo');

module.exports = function getVimeoLib () {
    return new Vimeo(config.get('vimeo.client_id'), config.get('vimeo.client_secret'));
};

Which my code can use like this:

var lib = require('./src/lib/vimeo.js').getVimeoLib();
// use the lib

from vimeo.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.