passport-slack
Passport strategy for authenticating with Slack
using the OAuth 2.0 API.
This module lets you authenticate using Slack in your Node.js applications.
By plugging into Passport, Slack authentication can be easily integrated into any application or framework that supports
Connect-style middleware, including
Express.
Install
$ npm install passport-slack-ponycode
Usage
Configure Strategy
The Slack authentication strategy authenticates users using a Slack account
and OAuth tokens. The strategy requires a verify
callback, which receives the
access token and corresponding secret as arguments, as well as user
which
contains the authenticated user's slack info. The verify
callback must
call done
providing a user to complete authentication.
In order to identify your application to Slack, specify the clientID,
clientSecret, and redirect URL within options
. The client ID and secret
are obtained by creating an application at
Slacks's api site.
var SlackStrategy = require('./passport-slack').SlackStrategy;
passport.use( 'slack', new SlackStrategy({
clientID: "[slack client id]",
clientSecret:"[slack client secret]",
callbackURL: "[host]/authenticate/callback",
slackTeam: "[optional slack team id to force joining a team]"
}, function( token, tokenSecret, profile, done ){
// This is your chance to find a User in your database.
var User = neoteric.model('User');
User.findBySlackId( profile.id, function( error, existingUser ){
if( error ) return done( error, false );
if( existingUser ){
done( error, existingUser );
}else{
var newUser = new User();
newUser.username = parsedBody.user;
newUser.slackId = slackId;
newUser.save( function( error, newUser ){
done( error, newUser );
});
}
});
}));
Authenticate Requests
Use passport.authenticate()
, specifying the 'slack'
strategy, to
authenticate requests.
For example, as route middleware in an Express
application:
app.get('/auth/slack',
passport.authenticate('slack'));
app.get('/auth/slack/callback',
passport.authenticate('slack', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/');
});
Credits
License
The MIT License
Copyright (c) 2014 Josh Kennedy <http://ponycode.com/>