Skip to content

microauth/microauth-twitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

microauth-twitter

Twitter oauth for micro

Build Status XO code style Greenkeeper badge

Add twitter authentication to your micro service as easy as a flick of your fingers. This module is a part of microauth collection.

Installation

npm install --save microauth-twitter
# or
yarn add microauth-twitter

Usage

app.js

const { send } = require('micro');
const microAuthTwitter = require('microauth-twitter');

const options = {
  consumerKey: 'CONSUMER_KEY',
  consumerSecret: 'CONSUMER_SECRET',
  callbackUrl: 'http://localhost:3000/auth/twitter/callback',
  path: '/auth/twitter'
};

const twitterAuth = 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
// }}
const handler = async (req, res, auth) => {

  if (!auth) {
    return send(res, 404, 'Not Found');
  }

  if (auth.err) {
    // Error handler
    return send(res, 403, 'Forbidden');
  }

  // save something in database here

  return `Hello ${auth.result.info.screen_name}`;

};

module.exports = twitterAuth(handler);

Run:

micro app.js

Now visit http://localhost:3000/auth/twitter

Author

Dmitry Pavlovsky