Github OAuth2 πŸ”‘ Guide

The GitHub provider lets you sign in πŸ”“ users using their GitHub accounts through OAuth2.


πŸ”§ GitHub App Setup Guide

To use GitHub OAuth2 in your app, you need to set up a GitHub App. Here's a step-by-step guide to obtain the necessary credentials (client_id, client_secret, redirect_uri) and configure your app.

Step 1: Register a new OAuth app

  1. Log in with your GitHub account or create one if you don't have it.

  2. Click on New OAuth app.

Step 2: Configure OAuth Settings

  1. Enter App name and Authorization callback URL

  2. Press Register

Step 3: Get Client_ID and Generate Client_Secret

  1. Copy the Client_ID

  2. Press Generate a new client secret

Step 4: Scopes for OAuth apps

Go get yourself familiarized with Scopes first.

Mostly scope='user,repo' will be enough for authentication as it returns really a lot of data for you to work with.

Step 6: Store Credentials Securely

⚠️ Note: It's best to store your GitHub App settings in a .env file for πŸ” security. Add the following to your .env file:

GITHUB_CLIENT_ID=your-client-id
GITHUB_CLIENT_SECRET=your-client-secret
GITHUB_REDIRECT_URI=https://localhost:8000/github/callback  #[//]: Change this to where you want Github to redirect the user after authentication
GITHUB_CLIENT_SCOPE="user,repo" # add other scope as you like

Use the python-dotenv package to load these variables in your Django project.

πŸš€ Getting Started

First, import the needed πŸ“¦ class and set up your GitHub App βš™οΈ settings:

Instead, refer to easy-way of GitHub Login setup for Django specific doc at here:

from omni_authify.providers import GitHub 

provider = GitHub(
    client_id='πŸ”‘ your-facebook-client-id', 
    client_secret='πŸ”’ your-facebook-client-secret',
    redirect_uri='🌐 your-facebook-redirect-uri',
    scope="user,repo"
)

βš™οΈ Updating Settings

⚠️ Note: Make sure that your redirect_uri matches the callback URL you set in your GitHub OAUTH2 app settings and in your Django URLs.


πŸ“‹ Methods

1. πŸ”— Get Authorization URL

This method creates the link πŸ”— you need to send the user to so they can log in using Facebook.

def get_authorization_url(state=None):
    pass

Parameters:

  • state (str, optional): A random string πŸ”€ to protect against cross-site request forgery attacks.

Returns:

  • str: The URL 🌐 to use for GitHub login.

Example:

auth_url = provider.get_authorization_url(state='random_state_string')

2. πŸ”“ Get Access Token

This method uses the code from GitHub to get an access token πŸ”‘.

def get_access_token(code):
    pass

Parameters:

  • code (str): The authorization code πŸ”’ you got from the callback URL.

Returns:

  • str: The access token πŸ”‘.

Example:

access_token = provider.get_access_token(code='authorization_code')

3. ** βš™οΈ Check to which scope your GitHub Oauth2 app has access.**

import os 
from dotenv import  load_dotenv

from omni_authify.providers.github import GitHub

load_dotenv()

github_client_id = os.getenv('GITHUB_CLIENT_ID')
github_client_secret = os.getenv('GITHUB_CLIENT_SECRET')
github_redirect_uri = os.getenv('GITHUB_REDIRECT_URI')
github_scope = os.getenv('GITHUB_SCOPE')

provider = GitHub(
    client_id=github_client_id,
    client_secret=github_client_secret,
    redirect_uri=github_redirect_uri,
    scope=scope
)

# ==== After obtaining an access token at Step 2 ====
scopes = provider.check_token_scopes(access_token)
print(scopes)

4. πŸ“„ Get User Profile

This method gets the user's profile information from Facebook.

def get_user_profile(access_token, fields="id,name,email,picture"):
    pass

Parameters:

  • access_token (str): The access token πŸ”‘ you got from get_access_token.

Returns:

  • dict: The user's profile information πŸ“‹.

Example:

user_info = provider.get_user_profile(access_token)

πŸ› οΈ Customizing Fields

For a comprehensive list of user profile fields and the necessary permissions, refer to the GitHub Scope Reference

Example:

user_info = provider.get_user_profile(access_token)

βœ… Best Practices

  • πŸ”’ Use Environment Variables: Always use environment variables to store important information like client_id and client_secret. This helps keep your credentials safe πŸ›‘οΈ.

  • πŸ”— Match Redirect URI: Make sure the redirect_uri is the same in both your GitHub App settings and your code to avoid errors 🚫 during the login process.

  • ⚠️ Error Handling: Handle any possible errors 🐞 during the login and token exchange process to ensure a smooth user experience 😊.


Now you're ready to use GitHub for authenticating users in your app πŸš€. Follow these steps and best practices to make sure everything runs securely πŸ” and smoothly ✨.

Final Result

Last updated

Was this helpful?