Overview
ThepublicGuard is an Angular route guard that protects public routes (like login and registration pages) from authenticated users. It redirects logged-in users to their appropriate dashboard based on their role, preventing them from accessing authentication pages when already authenticated.
Signature
Type Definition
Implementation
Parameters
Contains information about the route being activated
Contains the router state at a particular moment in time
Return Value
true- User is not authenticated, allow access to public routefalse- User is authenticated, redirect based on role
Redirect Behavior
When an authenticated user tries to access a public route, they are redirected based on their role:| Role | Redirect Destination |
|---|---|
ROLE_ADMIN | /libros (Books management) |
| Other roles | /catalogo (Public catalog) |
| No role | /catalogo (Public catalog) |
Usage
Route Configuration
Apply thepublicGuard to authentication and public routes:
Protecting Authentication Routes
The most common use case is protecting login/registration pages:How It Works
- Token Check (
src/app/core/guards/public-guard.ts:10): Retrieves authentication token fromTokenStorageService - Authentication Status:
- Authenticated (token exists):
- Retrieves user role from
localStorage(src/app/core/guards/public-guard.ts:15) - Redirects to role-specific route
- Returns
falseto block access
- Retrieves user role from
- Not Authenticated (no token):
- Returns
trueto allow access to the public route
- Returns
- Authenticated (token exists):
Dependencies
- TokenStorageService (
src/app/core/guards/public-guard.ts:6): Service for managing authentication tokens - Router (
src/app/core/guards/public-guard.ts:7): Angular router for navigation - localStorage: Browser API for retrieving user role
This guard implements the inverse logic of
authGuard. While authGuard protects authenticated routes, publicGuard protects public routes from authenticated users.When to Use
UsepublicGuard when you need to:
- Prevent logged-in users from accessing login/registration pages
- Implement automatic redirection based on user role after login
- Create landing pages that should only be visible to unauthenticated users
- Improve user experience by avoiding unnecessary authentication steps
User Experience Benefits
Better UX: If a user tries to access
/auth/login while already logged in, they are automatically redirected to their appropriate dashboard instead of seeing the login form unnecessarily.Example Scenarios
-
User bookmarks login page: An authenticated user who bookmarked
/auth/loginwill be automatically redirected to their dashboard instead of seeing the login form - Back button after login: If a user presses the back button after logging in, they won’t return to the login page but will be redirected appropriately
- Direct URL access: Authenticated users cannot manually navigate to authentication pages by typing the URL
Role-Based Redirection
The guard implements smart redirection based on user roles:Complete Authentication Flow
Combine all three guards for a complete authentication system:Related
- authGuard - Protects routes requiring authentication
- adminGuard - Protects routes requiring admin role
- TokenStorageService - Token management service