Overview
Biblioteca Virtual uses Angular’s standalone routing system with functional route guards. The routing configuration defines access control for public routes (login/register), authenticated user routes (catalog), and admin-only routes (management pages).Route Configuration
All routes are defined inapp.routes.ts with appropriate guards for access control:
src/app/app.routes.ts
Route Categories
Public Routes
Public routes are accessible only to unauthenticated users. ThepublicGuard redirects logged-in users to their appropriate dashboard.
When a logged-in user tries to access
/auth/login, they are automatically redirected to:/libros(admins)/catalogo(regular users)
Authenticated User Routes
Routes that require authentication but are available to both users and admins:User Catalog Route
Admin-Only Routes
Admin routes use bothauthGuard and adminGuard to ensure the user is authenticated AND has admin privileges:
- Books (Libros)
- Genres (Géneros)
- Loans (Préstamos)
Guard Execution Order
When multiple guards are specified, they execute in order from left to right:authGuard executes first
Checks if the user has a valid JWT token
- Pass: Continue to next guard
- Fail: Redirect to
/auth/login
Route Parameters
Edit routes use dynamic route parameters to identify which resource to edit:Accessing Route Parameters
In the component, access theid parameter using Angular’s ActivatedRoute:
Navigation Patterns
Programmatic Navigation
Navigate using Angular’sRouter service:
Template Navigation
UserouterLink directive in templates:
Redirect Behavior
Default Redirect
When users access the root path/, they are redirected to login:
404 Handling
Unknown routes redirect to login:Role-Based Redirects
ThepublicGuard implements intelligent redirects based on user role:
src/app/core/guards/public-guard.ts
Route Protection Summary
| Route Pattern | Guards | Access |
|---|---|---|
/auth/login | publicGuard | Unauthenticated users only |
/auth/registro | publicGuard | Unauthenticated users only |
/catalogo | authGuard | Any authenticated user |
/libros/* | authGuard, adminGuard | Admins only |
/autores/* | authGuard, adminGuard | Admins only |
/generos/* | authGuard, adminGuard | Admins only |
/prestamos | authGuard, adminGuard | Admins only |
Common CRUD Patterns
Most admin features follow this routing pattern:The same form component (
ResourceFormComponent) is used for both create and edit operations. The presence of an id parameter determines which mode to use.Best Practices
Always Use Guards
Every route should have appropriate guards to prevent unauthorized access
Guard Order Matters
Place
authGuard before adminGuard to ensure proper authentication checksReuse Form Components
Use the same component for create and edit by checking route parameters
Fallback Routes
Always define a wildcard route to handle 404 scenarios
Next Steps
Authentication
Learn how JWT authentication works
Authorization Guards
Deep dive into guard implementation
Navigation Component
Explore the navbar component
Form Components
Build CRUD forms with routing