Nocturnal Programmer's Machine
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

oauth-fetch-json

0.3.2 • Public • Published

This module simplifies the logic for making oauth authenticated requests for JSON. This is compatible with passport oauth strategies. If no oauth session is available, a standard fetch will be used.

Usage

import passport from 'passport'
import oauthFetch from 'oauth-fetch-json'

passport.use(
	new OAuthStrategy({
		consumerKey: CONSUMER_KEY,
		consumerSecret: CONSUMER_SECRET
	},
	function(token, tokenSecret, profile, done) {
		profile.oauth = {
			consumer_key : CONSUMER_KEY,
			consumer_secret : CONSUMER_SECRET,
			token : token,
			token_secret : tokenSecret
		}
		return done(null, profile);
	} )
);

const app = express()
function ensureAuthenticated(req, res, next) {
	if (req.isAuthenticated()) {
		return next();
	} else {
		res.status( 401 );
		res.send( 'Login required for this endpoint' );
	}
}
app.get('/oauth/endpoint', ensureAuthenticated, function(req, res){
		oauthFetch( 'http://endpointWhichYouAreOAuthenticatedAgainst.org', { query: 'foo' }, {}, req.user.oauth ).then( function ( data ) {
			res.setHeader('Content-Type', 'application/json');
			res.status( 200 );
			res.send( JSON.stringify( data ) );
		} );
	};
});

Keywords

none

install

npm i oauth-fetch-json

Downloadsweekly downloads

4

version

0.3.2

license

none

last publish

collaborators

  • avatar