Overview
Biblioteca Virtual implements JWT (JSON Web Token) authentication to secure API communications. The authentication system handles user login, registration, session management, and automatic token injection into HTTP requests.Authentication Architecture
1
User Login
User submits credentials through the login form
2
JWT Token Received
Backend validates credentials and returns JWT token with user data
3
Session Storage
Token and user data are stored in localStorage
4
Automatic Token Injection
HTTP interceptor adds token to all outgoing requests
5
Protected Route Access
Guards verify token presence before allowing route navigation
AuthService
TheAuthService manages all authentication operations:
src/app/auth/auth.service.ts
Key Methods
- login()
- register()
- saveSession()
- logout()
Sends username and password to the backend Parameters:
/auth/login endpoint.usuario- Object containingusernameandpassword
- Observable of
AuthResponsewith token and user data
Authentication Interfaces
Type-safe authentication data structures:src/app/core/models/auth.interface.ts
The
role field contains either ROLE_ADMIN or ROLE_USER, which is used throughout the application for authorization decisions.TokenStorageService
Manages JWT token persistence in localStorage:src/app/core/services/token-storage.service.ts
Token Storage Methods
HTTP Interceptor
TheauthInterceptor automatically attaches JWT tokens to all outgoing HTTP requests:
src/app/core/interceptors/auth-interceptor.ts
1
Interceptor Invoked
Every HTTP request triggers the interceptor
2
Token Retrieved
Gets JWT token from TokenStorageService
3
Header Added
If token exists, clones request and adds
Authorization: Bearer <token> header4
Request Sent
Modified request is sent to the backend
The interceptor is registered globally in
app.config.ts:Login Flow Example
Complete implementation of the login process:Login Component Example
Login Sequence Diagram
Registration Flow
User registration follows a similar pattern:Registration Example
Logout Implementation
Logout clears all session data and redirects to login:Logout Example
Session Persistence
Authentication data is stored inlocalStorage, making sessions persist across browser refreshes:
Stored Data
Checking Authentication Status
Error Handling
Handle authentication errors gracefully:Error Handling Pattern
Security Considerations
Token Expiration
JWT tokens have an expiration time set by the backend. Implement token refresh or force re-login on expiry.
HTTPS Only
Always use HTTPS in production to prevent token interception.
XSS Protection
Sanitize all user inputs to prevent token theft via XSS attacks.
Logout on Error
Clear session if backend returns 401/403 errors on protected endpoints.
Testing Authentication
Example unit tests for AuthService:auth.service.spec.ts
Next Steps
Authorization Guards
Learn how guards use authentication to protect routes
Auth Service
Explore backend API endpoints
User Roles
Understand role-based features
Error Handling
Implement robust error handling