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!
-
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
-
Require
httpandhttpsand create a global empty object called sessions:
var sessions = {};
-
Set up your handler and run your server
-
Create a login link for your ‘/’ url:
res.end('<a href=https://github.com/login/oauth/authorize? clientid=YourClientID><LOGIN</a>’)
-
require
querystringand create apostDatavariable which holds your id, secret and code. Remember to put your client id and client secret in aconfig.envfile! -
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
-
We created a cookie using a random number between 1 and 100 million, but there are better ways to make a cookie 🍪
-
Set a key-value pair in your
sessionsobject which corresponds to the cookie 🍪 andaccessToken -
Set your created cookie within your
res.writeHead -
End your respose with
(‘logged in’) -
use the
.endmethod after thehttps requestand give it(postData)as a parameter