{"id":20455368,"url":"https://github.com/alexmhack/django-auth","last_synced_at":"2025-10-16T21:57:23.536Z","repository":{"id":49693301,"uuid":"153724506","full_name":"Alexmhack/Django-Auth","owner":"Alexmhack","description":"Implementing social authentication in django using the famous django-allauth package","archived":false,"fork":false,"pushed_at":"2022-12-08T01:16:39.000Z","size":15,"stargazers_count":1,"open_issues_count":3,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-02T18:45:04.549Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Alexmhack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-19T03:59:55.000Z","updated_at":"2023-08-27T01:35:55.000Z","dependencies_parsed_at":"2023-01-24T02:30:35.449Z","dependency_job_id":null,"html_url":"https://github.com/Alexmhack/Django-Auth","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2FDjango-Auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2FDjango-Auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2FDjango-Auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2FDjango-Auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Alexmhack","download_url":"https://codeload.github.com/Alexmhack/Django-Auth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242006145,"owners_count":20056510,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T11:18:40.562Z","updated_at":"2025-10-16T21:57:23.447Z","avatar_url":"https://github.com/Alexmhack.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Django-Auth\nImplementing social authentication in django using the famous django-allauth package\n\n**I will be using [pipenv](https://pipenv.readthedocs.io/en/latest/) for my virtualenv stuff but you can use whatever package you want or completely skip a virtualenv (not recommended)**\n\n```\npipenv install django django-allauth\npipenv shell\ndjango-admin startproject mysite .\npython manage.py migrate\npython manage.py createsuperuser\npython manage.py runserver\n```\n\nNow for adding **django-allauth** features in the current django app we have to make \nsome changes in project settings and urls:\n\n**myproject/settings.py**\n```\nINSTALLED_APPS = [\n\t...\n    'django.contrib.sites',\t\t# \u003c- needed for allauth\n\n    # django-allauth\n    'allauth',\n    'allauth.account',\n    'allauth.socialaccount',\n    'allauth.socialaccount.providers.github',\n]\n```\n\nAnd in the same file in the very bottom add\n\n```\n# django-allauth\n\nAUTHENTICATION_BACKENDS = (\n\t# login by username backend from django\n    \"django.contrib.auth.backends.ModelBackend\",\n\n    # login by email backend by allauth\n    \"allauth.account.auth_backends.AuthenticationBackend\",\n)\n\n# id of the site that is created by default by 'django.contrib.sites'\nSITE_ID = 1\n\nLOGIN_REDIRECT_URL = 'home'\n```\n\nAfter adding the following settings make changes in **myproject/urls.py** for allauth\n\n```\nfrom django.contrib import admin\nfrom django.urls import path, include\n\nurlpatterns = [\n    path('admin/', admin.site.urls),\n\n    # django-allauth urls\n    path('accounts/', include('allauth.urls')),\n]\n```\n\nNow ```migrate``` to update the database\n\n```\npython manage.py migrate\npython manage.py runserver\n```\n\nGo to [127.0.0.1:8000/admin](http://127.0.0.1:8000/admin) and you will find the models \ncreated by ```django-allauth```\n\nGo to [Sites](http://127.0.0.1:8000/admin/sites/site/) and edit the existing site by\nchanging the ```Domain name:``` to ```127.0.0.1:8000``` and leaving the ```Display name:``` to the default of ```example.com```\n\nWhen you deploy your Django application live you’d replace ```127.0.0.1``` here and on \n**Github** with your actual production homepage. We use the localhost for testing \npurposes.\n\n# Github OAuth\nOAuth is an open standard for authentication between systems. When a user logs into our \nsite with their Github account, we will redirect them to Github which then sends us a \ntoken that represents the user.\n\nTo configure a new OAuth application on Github, go to [https://github.com/settings/applications/new](https://github.com/settings/applications/new)\n\nThe Application name is what the user will see is requesting permission to access their \nGithub account. The **Homepage URL** is as described. The **Authorization callback URL** takes a particular form for each integration as defined in the django-allauth [docs](http://django-allauth.readthedocs.io/en/latest/providers.html).\n\nAfter hitting the “Register application” button you’ll be redirected to the page \ncontaining the **Client ID** and **Client Secret**. Also note that in the real-world, \nyou’d never want to publicly reveal either of these keys!\n\nNow run the server if not running and navigate to [Social Applications](http://127.0.0.1:8000/admin/socialaccount/socialapp/) and ```ADD SOCIAL APPLICATION```\n\nWe’re using **Github** so that’s the **Provider**. We add a **name** and then the \n**Client ID** and **Secret ID** from Github. Final step is to add our site to the \n**Chosen sites** on the bottom. Then click **save.**\n\n# Home Page\nNow if you remember that we added a variable in settings ```LOGIN_REDIRECT_URL``` and \nits value as ```'home'```. This is the url that django will redirect to after logging \nthe user in after social login or the default login.\n\nSo let's create a simple class based view for home page\n\nCreate new file in **myproject** folder -\u003e ```views.py```\n```\nfrom django.views.generic import TemplateView\n\nclass Home(TemplateView):\n    template_name = 'home.html'\n```\n\nSince we gave a path for template, we also need to tell django about where the \ntemplates lies\n\n```\nTEMPLATES = [\n    {\n        'BACKEND': 'django.template.backends.django.DjangoTemplates',\n        'DIRS': [os.path.join(BASE_DIR, 'templates')],\t# \u003c- templates folder path\n        'APP_DIRS': True,\n        ...\n```\n\nCreate a folder **templates** in the path where ```manage.py``` file is located and \nplace the file ```home.html``` in it.\n\nCheckout my ```home.html``` file from repo, I have used some bootstrap to add styling.\n\n# Testing\nRun the server and logout from admin site and navigate to home url [127.0.0.1:8000](http://127.0.0.1:8000/) and you will find a **Sign Up** button. Click that button and \nyou will be redirected to sign up screen from github using your credentials. When \nauthorized you will be redirected again to home url and you can see your username \ndisplayed on the home page.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexmhack%2Fdjango-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexmhack%2Fdjango-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexmhack%2Fdjango-auth/lists"}