Skip to content

Instantly share code, notes, and snippets.

@agallant
Forked from shime/_readme.md
Created February 1, 2018 06:24
Show Gist options
  • Save agallant/b45ae4ba0d779b5b8345aa6ba3dc6b3b to your computer and use it in GitHub Desktop.
Save agallant/b45ae4ba0d779b5b8345aa6ba3dc6b3b to your computer and use it in GitHub Desktop.
github oauth in node using express

What?

Most basic example of authenticating with Github in node.

How?

Clone this gist, change keys inside config.js and then hit npm install && node app.js.

Done?

Now hit this URL: http://localhost:9292/auth/github

var express = require("express"),
app = express(),
config = require("./config.js")
port = 9292;
var githubOAuth = require('github-oauth')({
githubClient: config.GITHUB_KEY,
githubSecret: config.GITHUB_SECRET,
baseURL: 'http://localhost:' + port,
loginURI: '/auth/github',
callbackURI: '/auth/github/callback'
})
app.get("/auth/github", function(req, res){
console.log("started oauth");
return githubOAuth.login(req, res);
});
app.get("/auth/github/callback", function(req, res){
console.log("received callback");
return githubOAuth.callback(req, res);
});
githubOAuth.on('error', function(err) {
console.error('there was a login error', err)
})
githubOAuth.on('token', function(token, serverResponse) {
serverResponse.end(JSON.stringify(token))
})
var server = app.listen(port, function() {
console.log('Listening on port %d', server.address().port);
});
module.exports = {
'GITHUB_KEY': 'your-github-key-here',
'GITHUB_SECRET': 'your-github-secret-here'
}
{
"name": "example",
"dependencies": {
"express": "4.x.x",
"github-oauth": "0.x.x"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment