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
Log in with your GitHub account or create one if you don't have it.
Go to GitHub Developer Settings.
Click on New OAuth app.
Step 2: Configure OAuth Settings
Enter App name and Authorization callback URL
Press Register
Step 3: Get Client_ID and Generate Client_Secret
Copy the Client_ID
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.
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:
Note this way of using the library is not recommended!
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 fromget_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
andclient_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?