Adding social authentication to your site

You might also want to add social authentication to your site using services such as Facebook, Twitter, or Google. Python Social Auth is a Python module that simplifies the process of adding social authentication to our website. Using this module, you can let your users log in to your website using their account of other services. You can find the code of this module at https://github.com/python-social-auth.

This module comes with authentication backends for different Python frameworks, including Django. To install the Django package via pip, open the console and run the following command:

pip install social-auth-app-django==2.1.0

Then, add social_django to the INSTALLED_APPS setting in the settings.py file of your project:

INSTALLED_APPS = [
#...
'social_django',
]

This is the default application to add python-social-auth to Django projects. Now, run the following command to sync python-social-auth models with your database:

python manage.py migrate

You should see that the migrations for the default application are applied as follows:

Applying social_django.0001_initial... OK
Applying social_django.0002_add_related_name... OK
...
Applying social_django.0008_partial_timestamp... OK

Python-social-auth includes backends for multiple services. You can see a list of all backends at https://python-social-auth.readthedocs.io/en/latest/backends/index.html#supported-backends.

We will include authentication backends for Facebook, Twitter, and Google.

You will need to add social login URL patterns to your project. Open the main urls.py file of the bookmarks project and include the social_django URL patterns as follows:

urlpatterns = [
path('admin/', admin.site.urls),
path('account/', include('account.urls')),
path('social-auth/',
include('social_django.urls', namespace='social')),
]

Several social services will not allow the redirecting of users to 127.0.0.1 or localhost after a successful authentication. In order to make social authentication work, you will need a domain. In order to fix this, under Linux or macOS X, edit your /etc/hosts file and add the following line to it:

127.0.0.1 mysite.com

This will tell your computer to point the mysite.com hostname to your own machine. If you are using Windows, your hosts file is located at C:WindowsSystem32Driversetchosts.

To verify that your host redirection worked, start the development server with python manage.py runserver and open http://mysite.com:8000/account/login/ in your browser. You will see the following error:

Django controls the hosts able to serve your application using the ALLOWED_HOSTS setting. This is a security measure to prevent HTTP host header attacks. Django will only allow the hosts included in this list to serve the application. You can learn more about the ALLOWED_HOSTS setting at https://docs.djangoproject.com/en/2.0/ref/settings/#allowed-hosts.

Edit the settings.py file of your project and edit the ALLOWED_HOSTS setting as follows:

ALLOWED_HOSTS = ['mysite.com', 'localhost', '127.0.0.1']

Besides the mysite.com host, we explicitly include localhost and 127.0.0.1. We do this to be able to access the site through localhost, which is the default Django's behavior when DEBUG is True and ALLOWED_HOSTS is empty. Now, you should be able to open http://mysite.com:8000/account/login/ in your browser. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.205.27