Skip to content

Instantly share code, notes, and snippets.

@shime
Last active April 28, 2023 18:56
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