/microauth-slack

Slack oauth for micro

Primary LanguageJavaScriptMIT LicenseMIT

microauth-slack

Slack oauth for micro

Build Status XO code style Greenkeeper badge

Add slack 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-slack
# or
yarn add microauth-slack

Usage

app.js

const { send } = require('micro');
const microAuthSlack = require('microauth-slack');

const options = {
  clientId: 'CLIENT_ID',
  clientSecret: 'CLIENT_SECRET',
  callbackUrl: 'http://localhost:3000/auth/slack/callback',
  path: '/auth/slack',
  scope: 'identity.basic,identity.team,identity.avatar'
};

const slackAuth = microAuthSlack(options);

// Third `auth` argument will provide error or result of authentication
// so it will { err: errorObject} or { result: {
//  provider: 'slack',
//  accessToken: 'blahblah',
//  info: userInfo
// }}
const handler = async (req, res, auth) => {

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

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

  // Save something in database here

  return `Hello ${auth.result.info.user.name}`;

};

module.exports = slackAuth(handler);

Run:

micro app.js

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

Author

Artem Karpovich