Image from Samfield Hawb Bassey

Django Rest Auth for Django > 4.0

As we know django-rest-auth was a very capable library to handle authentication and social login system in REST API endpoints, but due to some latest updates in python and django django-rest-auth is no more working with latest versions of Django.

Solution

we have created a new library on the top of django-rest-auth to give support to latest version of python and django, completely working with no issues.

Installations

  1. install package:
pip install exarth-rest-auth

2. Add exarth_rest_auth app to INSTALLED_APPS in your django settings.py:

INSTALLED_APPS = (
...,
'rest_framework',
'rest_framework.authtoken',
...,
'exarth_rest_auth'
)

Note

This project depends on django-rest-framework library, so install it if you haven’t done yet. Make sure also you have installed rest_framework and rest_framework.authtokenapps

3. Add rest_auth urls:

urlpatterns = [
...
re_path(r'^rest-auth/', include('exarth_rest_auth.urls'))
]

4. Migrate your database

python manage.py migrate

You’re good to go now!

Registration (optional)

  1. If you want to enable standard registration process you will need to install django-allauth.
  2. Add django.contrib.sites, allauth, allauth.accountand exarth_rest_auth.registration apps to INSTALLED_APPS in your django settings.py:
  3. Add SITE_ID = 1 to your django settings.py
INSTALLED_APPS = (
...,
'django.contrib.sites',
'allauth',
'allauth.account',
'exarth_rest_auth.registration',
)
SITE_ID = 1

4. Add rest_auth.registration urls:

urlpatterns = [
...
re_path(r'^rest-auth/', include('exarth_rest_auth.urls')),
re_path(r'^rest-auth/registration/', include('exarth_rest_auth.registration.urls'))
]

Important Note

  1. exarth_rest_auth build on the top of django_rest_auth we have updated all the libraries and usage inside django_rest_auth with small code changes.
  2. just follow django-rest-auth documentation for futhur customization and changes.
  3. replace url with re_path whenever you face import url issue, django has removed url and replaced it with re_path
  4. Use latest features and functions of django inside your code if in case of errors, just like the above one, exarth_rest_auth supports all latest features of django.

Changes to Previous Project

  1. Replace rest_auth to exarth_rest_auth in your settings.py file

2. Replace rest_auth.urls to exarth_rest_auth.urls in your urls.py file

Keep code pythonists in the world’s most stable and loving web framework Django and Enjoy

JWT Support (JSON web token)

By default dj-rest-auth uses Django’s Token-based authentication. If you want to use JWT authentication, follow these steps:

pip Install djangorestframework-simplejwt

djangorestframework-simplejwt is currently the only supported JWT library.

Add a simple_jwt auth configuration to the list of authentication classes.

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES':(
'exarth_rest_auth.jwt_auth.JWTCookieAuthentication',
)
}

Add the following configuration value to your settings file to enable JWT authentication in dj-rest-auth.

REST_USE_JWT = True

Declare what you want the cookie key to be called. If you want to use the refresh token feature, also be sure to set that variable.

JWT_AUTH_COOKIE = ‘my-app-auth’

JWT_AUTH_REFRESH_COOKIE = ‘my-refresh-token’

This example value above will cause dj-rest-auth to return a Set-Cookie header that looks like this:

Set-Cookie: my-app-auth=xxxxxxxxxxxxx; expires=Sat, 28 Mar 2020 18:59:00 GMT; HttpOnly; Max-Age=300; Path=/

If JWT_AUTH_REFRESH_COOKIE is also set, it will also set a comparable cookie for that. JWT_AUTH_COOKIE is also used while authenticating each request against protected views.

Note

  • The remaining features are same as rest auth , the only difference is it support latest version of Django

Links

Github

Pypi

Exarth

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ikram Khan

I am an independent programmer with expertise in cloud development, web technologies and cross-platform desktop applications.