This project is a Django REST Framework (DRF) API with user authentication handled by Knox tokens. It includes user registration, login, logout, and fetching user data.
- User Registration
- User Login (using Knox token authentication)
- User Logout & Logout All Sessions
- Retrieve Authenticated User Data
- Secure password handling with Django's built-in authentication
git https://github.com/snipher-marube/knox-authentication-api.git
cd knox-authentication-api
python -m venv venv
source venv/bin/activate # On Windows use: venv\Scripts\activate
pip install -r requirements.txt
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
Endpoint: POST /api/auth/register/
Body:
{
"username": "testuser",
"email": "test@example.com",
"password": "testpassword"
}
Response:
{
"user": {
"id": 1,
"username": "testuser",
"email": "test@example.com"
},
"token": "knox-token"
}
Endpoint: POST /api/auth/login/
Body:
{
"username": "testuser",
"password": "testpassword"
}
Response:
{
"token": "knox-token"
}
Endpoint: GET /api/auth/user/
Headers:
Authorization: Token knox-token
Response:
{
"id": 1,
"username": "testuser",
"email": "test@example.com"
}
Endpoint: POST /api/auth/logout/
Headers:
Authorization: Token knox-token
Response:
{
"success": "Successfully logged out"
}
Endpoint: POST /api/auth/logoutall/
Headers:
Authorization: Token knox-token
Response:
{
"success": "Successfully logged out from all sessions"
}
-
Login Not Working in Postman?
- Ensure you're sending JSON data (not form-data).
- Set
Content-Type: application/json
in headers. - If using Postman, clear cookies and try again.
-
Knox Tokens Not Generated?
- Run migrations:
python manage.py migrate knox
- Ensure
rest_framework
settings include Knox authentication:REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'knox.auth.TokenAuthentication', 'rest_framework.authentication.BasicAuthentication', 'rest_framework.authentication.SessionAuthentication', ) }
- Run migrations:
-
Check Server Logs
- Add debug prints in
views.py
if needed. - Restart server after changes:
CTRL + C
thenpython manage.py runserver
.
- Add debug prints in
- Django - Web framework
- Django REST Framework (DRF) - API development
- Knox - Token-based authentication
- SQLite/PostgreSQL - Database (configurable)
This project is licensed under the MIT License.
For any issues, feel free to raise a GitHub issue or contact me at sniphermarube@gmail.com
. 🚀