Email + Social Logins in Django - Step-by-Step Guide


Email and social logins are common techniques for websites and apps to allow users to establish accounts or check in. Users must provide their email address and password for email login, however, social login allows users to sign in using their social networks accounts, such as Facebook or Google. In this tutorial, we'll go through how to set up email and social logins in Django.

Method

With Django, there are numerous ways to incorporate email and social logins. The Django-allauth package, which supports authentication for numerous login methods, including email and social logins, is one of the most prevalent. Another approach is to utilize the Django-social-auth package, which offers social authentication for a variety of social networking services.

Syntax

Django-allauth installation −

pip install django-allauth

Django-social-auth installation −

pip install django-social-auth

The following code illustrates the use of Django-allauth to implement email and social logins in Django −

Algorithm

  • Django-allauth or Django-social-auth should be installed.

  • In settings.py, configure authentication backends.

  • In urls.py, include allauth URLs.

  • To construct database tables, and run migrations.

  • Configure an email backend for email verification.

  • Modify templates to match the look of your website.

Include the following URLs in urls.py:

path('accounts/', include('allauth.urls')),

In urls.py, the line of code "path('accounts/', include('allauth.urls'))" contains the URLs given by the Django-allauth package, which manages authentication for multiple login methods, including email and social logins. This line of code translates the URL "accounts/" to the allauth URLs, allowing users to access the Django-allauth authentication views.

Run migrations to create the necessary database tables.

Set up the email backend in settings.py to enable email verification:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

Customize the templates in templates/account/ to match your site's design.

Follow these steps to create the project's environment and directory −

  • Python and pip should be installed.

  • With virtualenv or venv, create a virtual environment.

  • Turn on the virtual environment.

  • With pip, install Django.

  • With the "django-admin startproject" command, create a new Django project.

  • With the "python manage.py startapp" command, create a new Django app within the project.

  • In settings.py, add the new app to the INSTALLED APPS list.

  • In models.py, define the app's models.

  • Using the "python manage.py makemigrations" and "python manage.py migrate" commands, create the database tables.

  • In settings.py, configure the authentication backends.

  • Use the code line "path('accounts/', include('allauth.urls'))" to include the allauth URLs in urls.py.

  • In settings.py, configure an email backend for email verification.

  • Modify the templates in templates/account/ to match the style of your site.

  • Use "python manage.py runserver" to start the Django development server and test the email and social logins.

Example

The first piece of code (in settings.py) should be added to your project's settings.py file, which is where you establish the configuration settings for your Django project.

# settings.py
INSTALLED_APPS = [
   '...';
   'allauth',
   'allauth.account',
   'allauth.socialaccount',
   'allauth.socialaccount.providers.google',
   'allauth.socialaccount.providers.facebook',
   ...
]

AUTHENTICATION_BACKENDS = [
   'django.contrib.auth.backends.ModelBackend',
   'allauth.account.auth_backends.AuthenticationBackend',
]

SOCIALACCOUNT_PROVIDERS = {
   'google': {
      'APP': {
         'client_id': 'YOUR_CLIENT_ID',
         'secret': 'YOUR_SECRET_KEY',
         'key': ''
      }
   },
   'facebook': {
      'APP': {
         'client_id': 'YOUR_CLIENT_ID',
         'secret': 'YOUR_SECRET_KEY',
         'key': ''
      }
   }
} 

The second piece of code (in urls.py) should be added to your project's urls.py file, where you specify your Django project's URL patterns.

# urls.py
from django.urls import path, include

urlpatterns = [
   ...
   path('accounts/', include('allauth.urls')),
   ...
] 

Remember to change the "YOUR CLIENT ID" and "YOUR SECRET KEY" values with your real Google and Facebook developer console credentials. Finally, before executing your Django project, be sure to install any required packages (such as Django-allauth).

Output

Users may establish accounts or sign in using their email or social media accounts after integrating email and social logins using Django-allauth or Django-social-auth.

Conclusion

Using email and social logins in Django can improve the user experience and make the account creation process easier. Utilizing packages like Django-allauth or Django-social-auth can make the implementation process smoother and faster.

Updated on: 22-May-2023

276 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements