An open API service indexing awesome lists of open source software.

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.

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.