Passport strategy for authenticating with GitHub access tokens using the OAuth 2.0 API.
This module lets you authenticate using GitHub in your Node.js applications. By plugging into Passport, GitHub authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.
npm install passport-github-token
The GitHub authentication strategy authenticates users using a GitHub account and OAuth 2.0 tokens.
The strategy requires a verify
callback, which accepts these credentials and calls next
providing a user, as well as options
specifying a app ID and app secret.
var GitHubTokenStrategy = require('passport-github-token');
passport.use(new GitHubTokenStrategy({
clientID: GITHUB_CLIENT_ID,
clientSecret: GITHUB_CLIENT_SECRET,
passReqToCallback: true
}, function(req, accessToken, refreshToken, profile, next) {
User.findOrCreate({'github.id': profile.id}, function(error, user) {
return next(error, user);
});
}));
Use passport.authenticate()
, specifying the github-token
strategy, to authenticate requests.
For example, as route middleware in an Express application:
app.get('/auth/github', passport.authenticate('github-token'));
Or if you are using Sails framework:
// AuthController.js
module.exports = {
github: function(req, res) {
passport.authenticate('github-token', function(error, user, info) {
if (error) return res.serverError(error);
if (info) return res.unauthorized(info);
return res.ok(user);
})(req, res);
}
};
The request to this route should include a GET or POST data with the keys access_token
and optionally, refresh_token
set to the credentials you receive from GitHub.
GET /auth/github?access_token=<TOKEN>
If you receive a 401 Unauthorized
error, it is most likely because you have wrong access token or not yet specified any application permissions.
Once you refresh access token with new permissions, try to send this access token again.