Overview
Kioto Teteria Backend uses JWT (JSON Web Token) based authentication to secure API endpoints. The authentication system is built using NestJS’s Passport integration with thepassport-jwt strategy.
How It Works
The authentication flow consists of three main components:- Login endpoint - Validates credentials and issues JWT tokens
- JWT Strategy - Validates tokens and extracts user information
- JWT Guard - Protects routes requiring authentication
Login Flow
Admins authenticate by sending their email and password to the/auth/login endpoint:
AuthService validates the credentials against the database:
src/modules/auth/auth.service.ts:12
Password Security
Passwords are hashed using bcrypt before storage. The system usesbcrypt.compare() to validate passwords without ever storing them in plain text.
JWT Payload Structure
The JWT token contains the following claims:id- Admin user IDemail- Admin email addressrole- Admin role (used for authorization)
JWT Strategy
The JWT strategy extracts and validates tokens from the Authorization header:src/common/jwt.strategy.ts:12 for the complete implementation.
Token Extraction
Tokens are extracted from theAuthorization header using the Bearer scheme:
Protecting Routes
To protect routes, apply theJwtAuthGuard:
Configuration
JWT authentication is configured in theAuthModule:
src/modules/auth/auth.module.ts:8
Environment Variables
Secret key used to sign and verify JWT tokens. Should be a strong, randomly generated string.
Token Expiration
Tokens expire after 3 hours by default. This is configured in thesignOptions when registering the JWT module.
Login Request
Request Body
Response
Using the Access Token
Include the access token in the Authorization header for authenticated requests:Error Handling
The authentication system returns appropriate HTTP status codes:400 Bad Request- Invalid credentials or inactive account401 Unauthorized- Missing or invalid token
Security Considerations
Inactive admin accounts (where
isActive: false) cannot authenticate, even with valid credentials.Related
- Authorization - Learn about role-based access control
- Database - Understand the Admin model structure