0.1.1 • Published 7 years ago

microauth-twitter v0.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

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

0.1.1

7 years ago

0.1.0

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago