API Reference
Authentication API in Next-DRF
The Next-DRF framework provides a comprehensive set of authentication-related API endpoints, allowing secure user authentication and authorization. These endpoints manage tasks like user login, token refresh, and verification, ensuring the application remains secure and user sessions are managed efficiently.
Below is an overview of the key authentication-related endpoints and their functionality.
1. User Login Endpoint
1.1 Login User
Endpoint:
/api/users/login/
Method:
POST
Description: This endpoint allows users to authenticate by providing their username and password. On successful login, a pair of JSON Web Tokens (access and refresh tokens) is returned. The access token is used to access protected resources, while the refresh token is used to generate new access tokens.
Request Body:
{ "username": "john_doe", "password": "securepassword123" }
Response Example (Success -
200 OK
):{ "access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...", "refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..." }
2. Token Management Endpoints
2.1 Token Refresh
Endpoint:
/api/token/refresh/
Method:
POST
Description: This endpoint allows a user to refresh their access token using a valid refresh token. This is useful for maintaining a user's authenticated session without requiring them to log in again.
Request Body:
{ "refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..." }
Response Example (Success -
200 OK
):{ "access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..." }
2.2 Token Verification
Endpoint:
/api/token/verify/
Method:
POST
Description: This endpoint is used to verify the validity of an access or refresh token. It ensures that the provided token is not expired or tampered with, providing a layer of security for token-based authentication.
Request Body:
{ "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..." }
Response Example (Success -
200 OK
):{ "message": "Token is valid." }
3. Password Management Endpoint
3.1 Password Reset
Endpoint:
/api/users/reset-password/
Method:
POST
Description: This endpoint allows users to initiate a password reset process by providing their registered email address. On successful submission, a password reset link or code is sent to the provided email address.
Request Body:
{ "email": "john.doe@example.com" }
Response Example (Success -
200 OK
):{ "message": "Password reset link sent to the provided email." }
4. Securing Endpoints with JWT Authentication
4.1 Using JWT Tokens for Authentication
For endpoints that require user authentication, a JWT access token must be provided in the request headers. This token authenticates the user and ensures they have the necessary permissions to access the endpoint.
- Header Example:
Authorization: Bearer <access_token>
- Example Request (Get User Profile):
Endpoint:
/api/users/profile/
Method:
GET
Headers:
Authorization
:Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...
Response Example (Success -
200 OK
):{ "id": 1, "username": "john_doe", "email": "john.doe@example.com" }
Summary
The authentication-related endpoints in Next-DRF are designed to provide secure and efficient user management. By utilizing JWT tokens for access and refresh purposes, these endpoints facilitate robust user authentication, session management, and password recovery. Properly securing these endpoints ensures that sensitive user data is protected and that authenticated sessions remain valid without requiring constant re-authentication.
Understanding how to effectively use these endpoints allows for smooth integration between the frontend and backend, ensuring users can easily log in, stay authenticated, and manage their credentials securely.