Skip to content

Instantly share code, notes, and snippets.

@kevinbowen777
Last active June 19, 2022 10:11
Show Gist options
  • Select an option

  • Save kevinbowen777/e5cc993ea8223abbfeee6371faf35be5 to your computer and use it in GitHub Desktop.

Select an option

Save kevinbowen777/e5cc993ea8223abbfeee6371faf35be5 to your computer and use it in GitHub Desktop.

Revisions

  1. kevinbowen777 revised this gist May 4, 2022. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions django_social_login_integration.md
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,14 @@
    # Social Login integration with Django & allauth - 20220419
    ## Social Login integration with Django & allauth - 20220419

    ---

    References:
    Source code: https://github.com/pennersr/django-allauth
    Documentation: http://django-allauth.readthedocs.io/en/latest/providers.html
    Tutorial: https://learndjango.com/tutorials/django-allauth-tutorial
    --------------

    ---

    1. Install django-allauth package
    - `pipenv install django-allauth`
    OR
    @@ -23,6 +25,7 @@ INSTALLED_APPS = [
    # Third-party packages
    "allauth", # new
    "allauth.account", # new
    "allauth.socialaccount",
    "allauth.socialaccount.providers.github", # new
    # Local apps...,
    ]
    @@ -50,11 +53,12 @@ urlpatterns = [
    path("accounts/", include("allauth.urls")), # new
    path("", include("djangoblog.urls")),
    ]
    ```
    5. Update any templates containing 'login' or 'logout' to 'account_login'
    'account_logout' etc...

    6. Register a new GitHub OAuth application at https://github.com/settings/applications/new

    ```
    Application Name: Django Blog Application
    Homepage URL: http://127.0.0.1:8000
  2. kevinbowen777 created this gist Apr 26, 2022.
    90 changes: 90 additions & 0 deletions django_social_login_integration.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    # Social Login integration with Django & allauth - 20220419

    ---

    References:
    Source code: https://github.com/pennersr/django-allauth
    Documentation: http://django-allauth.readthedocs.io/en/latest/providers.html
    Tutorial: https://learndjango.com/tutorials/django-allauth-tutorial
    --------------
    1. Install django-allauth package
    - `pipenv install django-allauth`
    OR
    - `poetry add django-allauth`

    2. Add to `INSTALLED_APPS`
    ```
    # config/settings.py
    INSTALLED_APPS = [
    "django.contrib.admin,"
    ...,
    "django.contrib.sites", # new
    # Third-party packages
    "allauth", # new
    "allauth.account", # new
    "allauth.socialaccount.providers.github", # new
    # Local apps...,
    ]
    # At the bottom of the config, add:
    SITE_ID = 1
    LOGIN_REDIRECT_URL = "home"
    ACCOUNT_LOGOUT_REDIRECT_URL = "home"
    AUTHENTICATION_BACKENDS = (
    "django.contrib.auth.backends.ModelBackend",
    "allauth.account.auth_backends.AuthenticationBackend",
    )
    ACCOUNT_EMAIL_VERIFICATION = "none"
    EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
    ```

    3. Move templates to `account` directory
    `mv templates/registration/ templates/account` (N.B.: NO 's' at end of account)

    4. Update URLs
    ```
    # config/urls.py
    urlpatterns = [
    path("admin/", admin.site.urls),
    path("accounts/", include("allauth.urls")), # new
    path("", include("djangoblog.urls")),
    ]
    5. Update any templates containing 'login' or 'logout' to 'account_login'
    'account_logout' etc...
    6. Register a new GitHub OAuth application at https://github.com/settings/applications/new
    ```
    Application Name: Django Blog Application
    Homepage URL: http://127.0.0.1:8000
    Application Description:
    A basic poll application built with Django web framework
    Authorization callback URL:
    http://127.0.0.1:8000/accounts/github/login/callback/
    Enable Device Flow: Leave unchecked
    ```
    Click `register` button
    7. Copy Client ID and Client secret(you won't see it again!!!)
    Click `Update Application`
    8. login to http://127.0.0.1/admin
    - Select `sites`
    - Set Domain name to `127.0.0.1`
    - Go back to admin page
    - Select `Social Applications`
    enter the ID & secrets obtained in Step 7
    - Add our site to `Chosen Sites`
    9. `Edit templates/account/login.html`
    - Add at/near top of page:
    `{% load socialaccount %}`
    - Add button(s) after `</form>`:
    ```
    <p class="text-center text-muted">OR</p>
    <a class="btn btn-dark" href="{% provider_login_url 'github' %}">Continue with GitHub</a>
    ```
    10. Override the 'You are about to sign in using a third party account from GitHub.' template
    - `mkdir templates/socialaccount`
    - Create new `login.html` template
    11. Test & verify working