Token Configuration
JWT settings are configured inconfig/settings.py:208-214:
Access tokens expire after 1 hour, while refresh tokens remain valid for 3 days.
Obtain Token Pair
Exchange user credentials for access and refresh tokens.Endpoint
Permission
- AllowAny - No authentication required
Implementation
This endpoint usesTokenObtainPairView from rest_framework_simplejwt (see config/urls.py:36):
Request Body
Customer’s email address (used instead of username)
Customer’s password
The API uses email as the username field since
USERNAME_FIELD = "email" in the Customer model.Example Request
Success Response (200 OK)
JWT access token (valid for 1 hour)
JWT refresh token (valid for 3 days)
Error Responses
401 Unauthorized - Invalid Credentials
401 Unauthorized - Invalid Credentials
400 Bad Request - Missing Fields
400 Bad Request - Missing Fields
Refresh Access Token
Obtain a new access token using a valid refresh token.Endpoint
Permission
- AllowAny - No authentication required (but valid refresh token needed)
Implementation
This endpoint usesTokenRefreshView from rest_framework_simplejwt (see config/urls.py:37):
Request Body
Valid refresh token obtained from
/auth/token/ endpointExample Request
Success Response (200 OK)
New JWT access token (valid for 1 hour)
Error Responses
401 Unauthorized - Invalid Token
401 Unauthorized - Invalid Token
Using Access Tokens
Once you have an access token, include it in the Authorization header for all authenticated requests:Example Authenticated Request
Logout Implementation
Client-Side Logout
Token Best Practices
Store Tokens Securely
Store Tokens Securely
- Web apps: Use httpOnly cookies or secure session storage
- Mobile apps: Use secure keychain/keystore
- Never store tokens in localStorage for production apps (vulnerable to XSS)
Handle Token Expiration
Handle Token Expiration
Refresh Tokens Proactively
Refresh Tokens Proactively
Don’t wait for the access token to expire. Refresh it when you detect a 401 response or before expiry:
- Refresh when access token has < 5 minutes remaining
- Implement automatic retry logic for 401 responses
- Keep refresh token secure and use it only for token refresh
Update Last Login
Update Last Login
The API automatically updates the user’s
last_login field when obtaining tokens (configured via UPDATE_LAST_LOGIN: True in settings).Related Endpoints
The API also includes standard Django REST Framework authentication URLs at/auth/ (see config/urls.py:35):
/auth/login/- Browser-based login (for browsable API)/auth/logout/- Browser-based logout
These endpoints are primarily for the browsable API interface and are not typically used in production client applications.
Next Steps
Permission Classes
Learn about permission classes and access control for protected resources