Overview
TheauthGuard is a functional route guard that checks if a user is authenticated before allowing access to protected routes. If the user is not authenticated, they are redirected to the login page.
Type signature
Angular’s functional guard type that returns
true to allow navigation or a UrlTree to redirectHow it works
The auth guard follows this logic:- Security open check: If
environment.securityOpenistrue, all routes are accessible without authentication - Authentication check: Injects
AuthStoreand checks if the user is authenticated viaisAuthenticated() - Redirect: If not authenticated, creates a URL tree to redirect to
/auth/login
Configuration
When set to
true, bypasses authentication checks for all routes. Useful for development or testing.Usage
Apply the guard to routes in your route configuration:Source code
Dependencies
AuthStore- Provides authentication state and theisAuthenticated()methodRouter- Creates navigation URL trees for redirectsenvironment.securityOpen- Configuration flag to bypass authentication