https://github.com/ferryops/djangorestframework-simplejwt
This project is a Django-based API that uses `djangorestframework-simplejwt` for JSON Web Token (JWT) authentication. The configuration sets the access token expiration to 24 hours. The project includes user registration, login, and user profile endpoints.
https://github.com/ferryops/djangorestframework-simplejwt
Last synced: 12 months ago
JSON representation
This project is a Django-based API that uses `djangorestframework-simplejwt` for JSON Web Token (JWT) authentication. The configuration sets the access token expiration to 24 hours. The project includes user registration, login, and user profile endpoints.
- Host: GitHub
- URL: https://github.com/ferryops/djangorestframework-simplejwt
- Owner: ferryops
- Created: 2024-12-22T22:31:43.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-22T22:52:02.000Z (over 1 year ago)
- Last Synced: 2025-04-13T08:55:19.173Z (about 1 year ago)
- Language: Python
- Size: 20.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Django JWT Authentication Project
This project is a Django-based API that uses `djangorestframework-simplejwt` for JSON Web Token (JWT) authentication. The configuration sets the access token expiration to 24 hours. The project includes user registration, login, and user profile endpoints.
## Features
- Secure user authentication with JWT.
- 24-hour expiration for access tokens.
- Refresh tokens for prolonged access.
- Easy integration with Django Rest Framework (DRF).
- User registration and login endpoints.
- User profile endpoint for retrieving user information.
- Custom user model for user management.
## Installation
### 1. Clone the Repository
```bash
git clone https://github.com/ferryops/djangorestframework-simplejwt.git
cd djangorestframework-simplejwt
```
### 2. Create a Virtual Environment
```bash
python -m venv venv
source venv/bin/activate # On Windows: .\venv\Scripts\activate
```
### 3. Install Dependencies
```bash
pip install -r requirements.txt
```
### 4. Apply Migrations
```bash
python manage.py migrate
```
### 5. Run the Development Server
```bash
python manage.py runserver
```
## Configuration
### JWT Settings
The JWT settings are configured in `settings.py` using the `djangorestframework-simplejwt` library. The access token expiration is set to 24 hours:
```python
SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(hours=24),
'REFRESH_TOKEN_LIFETIME': timedelta(days=7),
'ROTATE_REFRESH_TOKENS': False,
'BLACKLIST_AFTER_ROTATION': True,
'ALGORITHM': 'HS256',
'SIGNING_KEY': 'your-secret-key',
}
```
### Authentication Classes
Ensure `JWTAuthentication` is added to the default authentication classes:
```python
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
),
}
```
## Endpoints
### Token Endpoints
| Endpoint | Method | Description |
| --------------------- | ------ | -------------------------------- |
| `/api/token/` | POST | Obtain access and refresh tokens |
| `/api/token/refresh/` | POST | Refresh the access token |
#### Example Request to Obtain Tokens
```bash
POST /api/token/
{
"username": "your_username",
"password": "your_password"
}
```
#### Example Response
```json
{
"access": "",
"refresh": ""
}
```
## Postman Collection
A Postman collection is provided in the repository to help you test the API endpoints. Import the collection into Postman to interact with the API.
[Django REST postman collection](https://github.com/ferryops/djangorestframework-simplejwt/blob/main/Django%20REST.postman_collection.json)
## Testing
To verify the expiration and functionality of tokens, use tools like Postman or curl to interact with the API. You can also decode the JWT using [jwt.io](https://jwt.io) to check the `exp` field.
## Dependencies
- asgiref==3.8.1
- Django==5.1.4
- django-cors-headers==4.6.0
- djangorestframework==3.15.2
- djangorestframework-simplejwt==5.3.1
- PyJWT==2.10.1
- sqlparse==0.5.3
## Contributing
Feel free to submit issues or pull requests for improvements or bug fixes.