Twitter oauth for micro
Add twitter authentication to your micro service as easy as a flick of your fingers. This module is a part of microauth collection.
npm install --save microauth-twitter
# or
yarn add microauth-twitterapp.js
const{ send }=require('micro');constmicroAuthTwitter=require('microauth-twitter');constoptions={consumerKey: 'CONSUMER_KEY',consumerSecret: 'CONSUMER_SECRET',callbackUrl: 'http://localhost:3000/auth/twitter/callback',path: '/auth/twitter'};consttwitterAuth=microAuthTwitter(options);// third `auth` argument will provide error or result of authentication// so it will { err: errorObject} or { result: {// provider: 'twitter',// accessToken: 'blahblah',// accessTokenSecret: 'blahblah',// info: userInfo// }}consthandler=async(req,res,auth)=>{if(!auth){returnsend(res,404,'Not Found');}if(auth.err){// Error handlerreturnsend(res,403,'Forbidden');}// save something in database herereturn`Hello ${auth.result.info.screen_name}`;};module.exports=twitterAuth(handler);Run:
micro app.jsNow visit http://localhost:3000/auth/twitter