PHP – Simple GitHub Login (OAuth)

If your codebase is in PHP and you would like user’s to be able to login to your site using GitHub (single sign-on) you can use the following as a good starting point:

Next you will need to setup an OAuth application within GitHub by doing the following:

  1. Login to GitHub.com.
  2. Go to your settings by clicking on your avatar image in the top-right corner of any GitHub page and then clicking on Settings in the drop-down menu.
  3. Scroll down to the Developer settings menu on the left and click OAuth Applications.
  4. Click the Register a new application button.
  5. Enter all of the necessary information to register your new OAuth application and then click Register application when you’re done.
  6. Take the Client ID and the Client Secret and place them in the constants defined in your PHP code from the beginning of this post.

That should be it. Now when you run this code you should be prompted to authorize your application to read your public information and private email addresses. You can remove the scope if you don’t need to be able to read any private information such as email addresses or you can change the scope after reviewing this page. If you want more information about OAuth apps for GitHub you can check out this page.

Happy coding! šŸ˜Ž