https://github.com/imerica/dj-rest-auth
Authentication for Django Rest Framework
https://github.com/imerica/dj-rest-auth
authentication django django-rest-framework jwt python rest-api
Last synced: 16 days ago
JSON representation
Authentication for Django Rest Framework
- Host: GitHub
- URL: https://github.com/imerica/dj-rest-auth
- Owner: iMerica
- License: mit
- Created: 2020-02-28T19:35:48.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2025-01-04T23:35:35.000Z (about 1 year ago)
- Last Synced: 2025-05-08T00:14:06.770Z (11 months ago)
- Topics: authentication, django, django-rest-framework, jwt, python, rest-api
- Language: Python
- Homepage: https://dj-rest-auth.readthedocs.io/en/latest/index.html
- Size: 816 KB
- Stars: 1,767
- Watchers: 20
- Forks: 329
- Open Issues: 236
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Authors: AUTHORS
Awesome Lists containing this project
README
# dj-rest-auth
[](https://github.com/iMerica/dj-rest-auth/actions/workflows/main.yml)
[](https://github.com/iMerica/dj-rest-auth/actions/workflows/security.yaml)
[](https://pypi.org/project/dj-rest-auth/)
[](https://pypi.org/project/dj-rest-auth/)
[](https://pypi.org/project/dj-rest-auth/)
Secure drop-in authentication endpoints for Django REST Framework. Works seamlessly with SPAs and mobile apps.
**[Documentation](https://dj-rest-auth.readthedocs.io/)** | **[PyPI](https://pypi.org/project/dj-rest-auth/)**
## Features
- Login, logout, password change, password reset
- User registration with email verification
- Built-in MFA/2FA support (TOTP + recovery codes)
- JWT authentication with HTTP-only cookies
- Social auth (Google, GitHub, Facebook) via django-allauth
- Fully customizable serializers
## Architecture
```mermaid
flowchart LR
Client[Client
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
```bash
pip install dj-rest-auth
```
```python
# settings.py
INSTALLED_APPS = [
...
'rest_framework',
'rest_framework.authtoken',
'dj_rest_auth',
]
```
```python
# 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
```bash
pip install dj-rest-auth djangorestframework-simplejwt
```
```python
# 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
```bash
pip install 'dj-rest-auth[with-social]'
```
```python
# settings.py
INSTALLED_APPS = [
...
'django.contrib.sites',
'allauth',
'allauth.account',
'dj_rest_auth.registration',
]
SITE_ID = 1
```
```python
# urls.py
urlpatterns = [
path('auth/', include('dj_rest_auth.urls')),
path('auth/registration/', include('dj_rest_auth.registration.urls')),
]
```
## MFA / 2FA
```bash
pip install 'dj-rest-auth[with-mfa]'
```
MFA ships as an opt-in sub-package (`dj_rest_auth.mfa`) with:
- TOTP login challenge flow
- Recovery codes
- Security-focused defaults (short-lived MFA tokens, activation confirmation)
See the guide for setup and endpoint details:
[MFA Guide](https://dj-rest-auth.readthedocs.io/en/latest/guides/mfa/)
## Documentation
Full documentation at **[dj-rest-auth.readthedocs.io](https://dj-rest-auth.readthedocs.io/)**
- [Installation & Configuration](https://dj-rest-auth.readthedocs.io/en/latest/getting-started/installation/)
- [API Endpoints](https://dj-rest-auth.readthedocs.io/en/latest/api/endpoints/)
- [JWT & Cookies Guide](https://dj-rest-auth.readthedocs.io/en/latest/guides/jwt-cookies/)
- [Social Authentication](https://dj-rest-auth.readthedocs.io/en/latest/guides/social-auth/)
- [MFA Guide](https://dj-rest-auth.readthedocs.io/en/latest/guides/mfa/)
## Contributing
```bash
pip install -r dj_rest_auth/tests/requirements.txt
python runtests.py
```
See [Contributing Guide](https://dj-rest-auth.readthedocs.io/en/latest/contributing/) for details.
## License
MIT