Introduction
The Library Management API uses a robust JWT (JSON Web Token) based authentication system to secure endpoints and manage user sessions. The authentication mechanism is stateless, meaning no session data is stored on the server.Authentication Architecture
The authentication system is built on the following components:User Registration
New users register by providing their credentials, which are securely hashed using BCrypt before storage.
User Login
Users authenticate with their username and password. Upon successful authentication, a JWT token is generated.
Token Validation
Each protected endpoint request must include the JWT token in the Authorization header. The token is validated before processing the request.
Key Features
Stateless Authentication
The API uses stateless authentication, meaning:- No server-side session storage
- JWT tokens contain all necessary authentication information
- Tokens are self-contained and verifiable
- Session management is handled client-side
BCrypt Password Hashing
Passwords are never stored in plain text:- BCrypt algorithm provides secure one-way hashing
- Each password is salted automatically
- Brute-force attacks are computationally expensive
JWT Token Security
Tokens are signed and validated:- HMAC256 algorithm ensures token integrity
- Tokens include expiration time to limit validity
- Issuer verification prevents token forgery
- Unique JWT ID (jti) for each token
Security Configuration
The API implements several security measures:The security configuration disables CSRF protection since the API is stateless and uses JWT tokens for authentication.
Public vs Protected Endpoints
Public Endpoints (No Authentication Required)
POST /api/v1/auth/register- User registrationPOST /api/v1/auth/login- User login- Swagger UI documentation endpoints
Protected Endpoints (JWT Token Required)
- All
/api/v1/users/**endpoints - All
/api/v1/books/**endpoints - Any endpoint not explicitly marked as public
Authentication Flow Diagram
Token Lifecycle
- Token Generation: Created upon successful login with user credentials
- Token Usage: Included in Authorization header for subsequent requests
- Token Validation: Verified on each request to protected endpoints
- Token Expiration: Automatically expires after 30 minutes (1800000 ms)
- Token Refresh: Client must re-authenticate to obtain a new token
Tokens expire 30 minutes after issuance. Plan your client-side token refresh strategy accordingly.
Next Steps
JWT Tokens
Learn about JWT token structure, claims, and validation
User Registration
Understand the registration process and requirements
User Login
Explore the login flow and token generation
API Endpoints
View detailed API endpoint documentation