- Django
- Django-allauth
- Social Account Credentials (Gaogle,Github,Facebook,Twitter)
$ mkdir googleauth
$ cd googleauth
$ python -m venv socialenv
$ source socialenv/bin/activate
$ pip install django==4.20.0 django-allauth==0.57.0
$ django-admin startproject sociallogin
$ cd sociallogin
$ python manage.py startapp users_login
Configure newly apps users_login
in sociallogin
project. Open settings.py
from sociallogin projects and configure below.
# Add settings.py inside sociallogin/socialallogin
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users_login', # Users Login apps which was created
]
INSTALLED_APPS = [
...
'users_login',
'django.contrib.sites', # apps for social sites configuration
'allauth', # apps for all authentication
'allauth.account', # # apps for account auth
'allauth.socialaccount', # apps for socialaccount
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'allauth.account.middleware.AccountMiddleware', # middleware for django-allauth
]
# Used for django.contrib.sites configuration
SITE_ID = 1
$ python manage.py migrate
$ python manage.py createsuperuser --username=admin --email=admin@gmail.com
$ python manage.py runserver
- Create Google API for Application
- Configure Google API with Django Application
You'll need to set up an OAuth application via Google Developers Console to add a Google login to your app. We start by creating a new project:
Download Google Client ID and Client Secret from download option
# Add google provider apps in django settings.py
INSTALLED_APPS = [
...
'allauth.socialaccount.providers.google', # add providers for google api
]
# Add following changees on settings.py
AUTHENTICATION_BACKENDS = [
# Default backend for Django authentication.
'django.contrib.auth.backends.ModelBackend',
# Allauth specific authentication methods, such as login by e-mail.
'allauth.account.auth_backends.AuthenticationBackend',
]
# LOGIN_REDIRECT_URL, destination of login page in your urls.py
LOGIN_REDIRECT_URL = '/'
# LOGOUT_REDIRECT, where to redirect when user logout
LOGOUT_REDIRECT_URL = '/'
# providers which will use as provider in template
SOCIALACCOUNT_PROVIDERS = {
'google': {
'SCOPE': ['profile', 'email'],
'AUTH_PARAMS': {'access_type': 'online'},
}
}
# Create new urls.py in sociallogin/users_login/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('logout/', views.logout_view, name='logout_view'),
]
# Add urls.py in sociallogin/sociallogin/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('account/', include('allauth.urls')), # Add this line
path('', include('users_login.urls')), # Add this line
]
# Add in views.py
from django.shortcuts import render, redirect
from django.contrib.auth import logout
def home(request):
return render(request, 'home.html')
def logout_view(request):
logout(request)
return redirect('/')
Add templates for home
and logout_view
views. Create a home.html
in sociallogin/users_login/templates/home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Social Login</title>
</head>
<body>
{% load socialaccount %}
{% if user.is_authenticated %}
<p>User is authenticated as {{ user.username }}.</p>
<a href="logout/">Logout</a>
{% else %}
<h1>Social Login</h1>
<a href="{% provider_login_url 'google' %}?next=/">Login</a>
{% endif %}
</body>
</html>
Finally Run the Application
$ python manage.py runserver