This Django application demonstrates how to set up token authentication using Djoser and a custom user model named "User" in the "users" app.
- Python 3.x
- Django
- Djoser
-
Clone the Repository:
git clone https://github.com/yourusername/django-users-djoser-token-auth.git
-
Install Dependencies:
pip install -r requirements.txt
-
Run Migrations:
python manage.py migrate
-
Create a Superuser:
python manage.py createsuperuser
-
Run the Development Server:
python manage.py runserver
The application will be accessible at http://localhost:8000/
The Djoser configuration is set up in the settings.py
file within the "users" app. Ensure that the following configurations are correctly set:
INSTALLED_APPS = [
# ...
'djoser',
'users',
# ...
]
# ...
DJOSER = {
'SERIALIZERS': {
'user_create': 'users.serializers.CustomUserCreateSerializer',
'user': 'users.serializers.CustomUserSerializer',
'token_create': 'djoser.serializers.TokenCreateSerializer',
},
# Other Djoser settings...
}
-
User Registration:
Endpoint:
/auth/users/
Use a POST request with the following payload:
{ "email": "[email protected]", "password": "yourpassword", "first_name": "John", "last_name": "Doe", "bio": "A brief bio", "date_of_birth": "1990-01-01" }
-
User Login:
Endpoint:
/auth/token/login/
Use a POST request with the following payload:
{ "email": "[email protected]", "password": "yourpassword" }
The response will include an access token.
-
Access Protected Endpoints:
Include the access token in the Authorization header to access protected endpoints.
curl -H "Authorization: Token <your-access-token>" http://localhost:8000/protected-endpoint/
-
Superuser Access:
Superuser credentials can be used to access all endpoints and perform administrative actions.
The custom user model is defined in users/models.py
. Ensure that the AUTH_USER_MODEL
setting in settings.py
is correctly set to 'users.User'
.
# settings.py
AUTH_USER_MODEL = 'users.User'
Feel free to contribute to this project by submitting bug reports, feature requests, or pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.
Replace "yourusername" with your actual GitHub username in the clone URL, and customize the file paths, payload examples, and other details according to your specific application.