Overview
Biblioteca Virtual implements role-based access control (RBAC) using Angular’s functional route guards. Three guards work together to control access to different parts of the application based on authentication status and user roles.Guard Types
authGuard
Ensures user is authenticated
adminGuard
Verifies user has admin role
publicGuard
Restricts access to unauthenticated users
authGuard - Authentication Check
TheauthGuard verifies that a user has a valid JWT token before accessing protected routes.
src/app/core/guards/auth-guard.ts
How It Works
1
Token Retrieval
Gets JWT token from
TokenStorageService2
Token Validation
Checks if token exists (non-null)
3
Decision
- Token exists: Returns
true, allows navigation - No token: Redirects to
/auth/login, returnsfalse
Usage Example
app.routes.ts
authGuard does NOT check user roles. It only verifies authentication. Use adminGuard for role-based restrictions.adminGuard - Role Verification
TheadminGuard checks if the authenticated user has the ROLE_ADMIN role.
src/app/core/guards/admin-guard.ts
How It Works
1
Role Retrieval
Reads user role from localStorage
2
Role Comparison
Checks if role equals
'ROLE_ADMIN'3
Decision
- Is Admin: Returns
true, allows navigation - Not Admin: Redirects to
/catalogo, returnsfalse
Usage Example
app.routes.ts
publicGuard - Unauthenticated Access
ThepublicGuard restricts access to routes that should only be available to unauthenticated users (like login and registration pages).
src/app/core/guards/public-guard.ts
How It Works
1
Token Check
Checks if user has a JWT token
2
Role-Based Redirect
If logged in, redirects based on role:
- Admin:
/libros(Book management) - User:
/catalogo(Book catalog)
3
Access Control
- Logged in: Returns
false, blocks access to public routes - Not logged in: Returns
true, allows access
Usage Example
app.routes.ts
This prevents logged-in users from accessing the login page, improving UX by automatically redirecting them to their appropriate dashboard.
Guard Combination Patterns
Public Routes
Routes accessible only to unauthenticated users:Authenticated User Routes
Routes accessible to any logged-in user (admin or regular user):Admin-Only Routes
Routes accessible only to authenticated admins:Guard Execution Flow
When multiple guards are specified, they execute sequentially:- Success Path
- Auth Failure
- Role Failure
Role-Based Navigation in Components
Show/hide UI elements based on user role:Conditional Navigation
Template with Role Checks
User Roles
The application supports two user roles:- ROLE_ADMIN
- ROLE_USER
Permissions:
- Access all authenticated routes
- Manage books (CRUD operations)
- Manage authors (CRUD operations)
- Manage genres (CRUD operations)
- View and manage loans
- Access user catalog
/librosProgrammatic Role Checks
UseAuthService methods to check roles in component logic:
Component Logic
Guard Testing
Example unit tests for guards:auth-guard.spec.ts
admin-guard.spec.ts
Security Best Practices
Server-Side Validation
Always validate permissions on the backend. Guards only control UI access, not API security.
Guard Order
Place
authGuard before adminGuard to avoid checking roles for unauthenticated users.Token Validation
Implement token expiration handling and refresh mechanisms.
Hide UI Elements
Use role checks to hide admin features from regular users in the UI.
Common Scenarios
Scenario 1: Unauthenticated User
Scenario 2: Regular User Accessing Admin Route
Scenario 3: Admin User
Scenario 4: Logged-In User Visits Login
Advanced: Custom Guards
Create custom guards for specific business logic:Custom Permission Guard
Next Steps
Routing
Review complete route configuration
Authentication
Learn about JWT authentication flow
User Management
Manage user accounts and roles
Error Handling
Handle authorization errors gracefully