Skip to content

Instantly share code, notes, and snippets.

@kushsolitary
Created June 18, 2013 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kushsolitary/5806788 to your computer and use it in GitHub Desktop.
Save kushsolitary/5806788 to your computer and use it in GitHub Desktop.
Created using Inkpen - http://inkpen.in

Implementing Github OAuth in NodeJS / ExpressJS Application

znxbcmbxcmz

Few months ago, Rishabh posted about implementing Twitter OAuth in your NodeJS / ExpressJS app using the Node-oauth library. However, for implementing Github OAuth, you wont need any extra library to make it work. Although, you can use one (more about it later) to make your work easy. Please note that you can use the techniques described here in any app because the method of implementation is same. So lets get started.

Add your application to Github

Few days ago, I launched Inkpen with Github OAuth as an option for authentication. The app is created using ExpressJS and setting up Github OAuth was really a very easy part. First of all, youll need to add your application to your Github account. So, head over to this page and fill out the form like shown below.

Well use the above Callback URL as a route in our application in order to receive the access_token of the user. After adding the application to your github account, youll receive your super secret client_id and client_secret strings looking something like this:

The above details are just for an example so yours might look a little different. Add them to your config file or put them somewhere safe where no one except you could see it. Now, well start adding routes in our expressjs application.

Adding routes

Lets create our first route. In the first route, what well do is simply redirect the user to the githubs oauth authorization page (Which shows the Accept/Reject application access buttons etc). If the user accepts it, then github will send a GET request to our callback (which we added earlier when setting up the app on Github).

Heres the code for our first route:

app.get(/auth/github, function(req, res) {
	url = https://github.com/login/oauth/authorize/?client_id= + config.github_client_id + &scope=gist;
	res.redirect(url);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment