Skip to main content

Authentication and Registration in Django Rest Framework

Project description

dj-rest-auth

CI PyPI Python Django

Drop-in authentication endpoints for Django REST Framework. Works seamlessly with SPAs and mobile apps.

Documentation | PyPI

Features

  • Login, logout, password change, password reset
  • User registration with email verification
  • JWT authentication with HTTP-only cookies
  • Social auth (Google, GitHub, Facebook) via django-allauth
  • Fully customizable serializers

Architecture

flowchart LR
    Client[Client<br/>React / Vue / Mobile]
    
    subgraph Django
        subgraph dj-rest-auth
            Auth[Login / Logout]
            Reg[Registration]
            PW[Password Reset]
        end
        
        DRF[Django REST Framework]
        DJAuth[django.contrib.auth]
        AA[django-allauth]
        JWT[simplejwt]
    end
    
    Client <--> dj-rest-auth
    
    Auth --> DRF
    Auth --> DJAuth
    Auth -.-> JWT
    Reg -.-> AA
    PW --> DJAuth

Quick Start

pip install dj-rest-auth
# settings.py
INSTALLED_APPS = [
    ...
    'rest_framework',
    'rest_framework.authtoken',
    'dj_rest_auth',
]
# urls.py
urlpatterns = [
    path('auth/', include('dj_rest_auth.urls')),
]

You now have:

Endpoint Method Description
/auth/login/ POST Obtain auth token
/auth/logout/ POST Revoke token
/auth/user/ GET, PUT User details
/auth/password/change/ POST Change password
/auth/password/reset/ POST Request reset email
/auth/password/reset/confirm/ POST Confirm reset

JWT with HTTP-only Cookies

pip install dj-rest-auth djangorestframework-simplejwt
# settings.py
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'dj_rest_auth.jwt_auth.JWTCookieAuthentication',
    ],
}

REST_AUTH = {
    'USE_JWT': True,
    'JWT_AUTH_COOKIE': 'access',
    'JWT_AUTH_REFRESH_COOKIE': 'refresh',
    'JWT_AUTH_HTTPONLY': True,
}

Registration

pip install 'dj-rest-auth[with-social]'
# settings.py
INSTALLED_APPS = [
    ...
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'dj_rest_auth.registration',
]

SITE_ID = 1
# urls.py
urlpatterns = [
    path('auth/', include('dj_rest_auth.urls')),
    path('auth/registration/', include('dj_rest_auth.registration.urls')),
]

Documentation

Full documentation at dj-rest-auth.readthedocs.io

Contributing

pip install -r dj_rest_auth/tests/requirements.txt
python runtests.py

See Contributing Guide for details.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dj_rest_auth-7.1.1.tar.gz (420.1 kB view details)

Uploaded Source

File details

Details for the file dj_rest_auth-7.1.1.tar.gz.

File metadata

  • Download URL: dj_rest_auth-7.1.1.tar.gz
  • Upload date:
  • Size: 420.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for dj_rest_auth-7.1.1.tar.gz
Algorithm Hash digest
SHA256 33dc190d3007aef6658db53352a6dc214e5d464e7efd97589dc2bb81ea1d5476
MD5 e984307ed7ece620de2f08a57208fb96
BLAKE2b-256 9fe083a98a31d2398b310044f5f0060a1ae2bbdbaeece54761b1a2b3dd433bfb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page