Package: django-rest-github-oauth
A simple python library to authenticate users with github in Django applications.Free software: MIT licenseDocumentation:https://django-rest-github-oauth.readthedocs.io.RequirementsPython (3.7, 3.8, 3.9)Django (2.x, 3.x, 4.x)Django REST Framework (3.10, 3.11, 3.12)SetupInstall the package in your python environment usingpip.pipinstalldjango-rest-github-oauthFor detailed information regarding the installation, see theinstallation guide.Then your django project must be configured to use the library. For that include it
inINSTALLED_APPSof yoursettings.py. Besides that, you will have to include the auth handler app in installed apps.Currently,django-rest-github-oauthsupports the following authentication backends:Token Authentication usingdjangorestframework token authenticationJWT Authentication usingdjangorestframework simple jwtUsing JWTINSTALLED_APPS=[# ...'rest_framework_simplejwt','django_rest_github_oauth',# ...]Using Token AuthenticationINSTALLED_APPS=[# ...'rest_framework.authtoken','django_rest_github_oauth',# ...]NoteIf you are using Token Authentication, please header over todjangorestframework token authenticationand add related settings to yoursettings.pyIf you are using JWT Authentication, please header over todjangorestframework simple jwtand add related settings to yoursettings.pyThen add the following to yoursettings.py:GITHUB_AUTH_KEY="<your_github_app_key>"GITHUB_AUTH_SECRET="<your_github_app_secret>"GITHUB_AUTH_USE_JWT=True# False if you're using token based authenticationGITHUB_AUTH_CALLBACK_URL="http://localhost:3000/auth/success/"# url of the frontend handling redirects from githubGITHUB_AUTH_ALLOWED_REDIRECT_URIS=[GITHUB_AUTH_CALLBACK_URL]NoteIf you have not yet created an app on github, you need to create an app to
get the client id and secret.register app with github.WarningYou need to set theGITHUB_AUTH_CALLBACK_URLthe same value you set ongithubwhile creating the app.Then add the following to you mainurls.pyfile.urlpatterns=[# ...path('admin/',admin.site.urls),path('auth/github/',include('django_rest_github_oauth.urls')),# ...]Run migrationspythonmanage.pymigrateThat’s all you have to do on the backend.UsageTo getauthorizaition_uri, make aGETrequest to the following url:http://localhost:8000/auth/github?redirect_uri=http://localhost:3000/auth/success/This will return a payload of the form{"data":{"authorization_uri":"https://github.com/login/oauth/authorize?client_id=shlf898f7dsfsd0f90wer9fs&redirect_uri=http://localhost:3000/auth/success/&state=dac7944888d140e19280&response_type=code&scope=user:email,read:user"},"message":"success","error":false}Redirect your user toauthorization_uri.Then, after the user has authorized your app, they will be redirected to theGITHUB_AUTH_CALLBACK_URLyou specified with two query parameters:codestateIn your frontend javascript, read those query parameters. Here is a quick snippet how you can achieve that.constquery=newURLSearchParams(window.location.search);constcode=query.get("code");conststate=query.get("state");Then make aPOSTrequest to the following url withcodeandstatein the request body:http://localhost:8000/auth/githubThe reuturn payload will have user informations and appropriate tokens.Here is a snippet how you can make call usingaxios.constquery=newURLSearchParams(window.location.search);constcode=query.get("code");conststate=query.get("state");constdetails={code:code,state:state,};consturl="http://127.0.0.1:8000/auth/github/";axios({method:"post",url:url,data:details,}).then((response)=>{console.log(response)// login the user and save token for further request to the backend}).catch((err)=>console.log(err));History0.1.2 (2022-02-26)Addedgithub_user_createdsignal.0.1.1 (2022-02-04)Minor updates.No explicitmakemigrationsrequired.0.1.0 (2022-02-02)First release on PyPI.Authentication with github.Create user account with github.Track state.