Skip to content

Latest commit

 

History

History
35 lines (18 loc) · 1.91 KB

README.md

File metadata and controls

35 lines (18 loc) · 1.91 KB

OAuth2 Tutorial

The following is a walkthrough to set up a simple OAuth2 authenticated application. We recommend you do this tutorial in a 🍐

Come join our chat to discuss cookies and authentication!

Join the chat at https://gitter.im/Conorc1000/Oauth2Tutorial

  1. Add a developer application on Github and give the URL of your page to generate client id (use your localhost address as your homepage URL for now). Set ‘authorisation callback URL’ to the page you want the user to be redirected to after they have logged in. Once you have done this, github will generate a client ID and client secret

  2. Require http and https and create a global empty object called sessions:

var sessions = {};

  1. Set up your handler and run your server

  2. Create a login link for your ‘/’ url:

res.end('<a href=https://github.com/login/oauth/authorize? clientid=YourClientID><LOGIN</a>’)

  1. require querystring and create a postData variable which holds your id, secret and code. Remember to put your client id and client secret in a config.env file!

  2. Create an https request to github with the hostname, path and method and an anonymous function for the response. This function should retrieve the access token from the github chunk

  3. We created a cookie using a random number between 1 and 100 million, but there are better ways to make a cookie 🍪

  4. Set a key-value pair in your sessions object which corresponds to the cookie 🍪 and accessToken

  5. Set your created cookie within your res.writeHead

  6. End your respose with (‘logged in’)

  7. use the .end method after the https request and give it (postData) as a parameter

Congrats!