Slack oauth for micro
Add slack 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-slack
# or
yarn add microauth-slack
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