signedInGuard
Protects routes that require an authenticated user. Redirects to the sign-in page if the user is not authenticated.Type Signature
Parameters
The activated route snapshot (unused in this guard)
The current router state, used to capture the return URL for redirects
Return Value
ReturnsPromise<GuardResult>:
trueif the user is authenticated- A
UrlTreeredirecting to/sign-inwith areturnUrlquery parameter if not authenticated
Behavior
- Injects required services:
Router,AlertService,LoggerService, andUserService - Attempts to retrieve user claims via
userService.getClaims() - If claims exist (user is authenticated), allows navigation
- If claims are null, redirects to
/sign-inwith the current URL as a return parameter - Handles exceptions by logging errors and displaying alerts
Usage
Real-World Examples
ThesignedInGuard is used in Jet to protect:
/profile- User profile management/update-password- Password update functionality
Source Code
signedOutGuard
Protects routes that should only be accessible to unauthenticated users (e.g., sign-in, sign-up). Redirects to the home page if the user is already authenticated.Type Signature
Parameters
No parameters are used by this guard.Return Value
ReturnsPromise<GuardResult>:
trueif the user is not authenticated- A
UrlTreeredirecting to/if the user is authenticated
Behavior
- Injects required services:
Router,AlertService,LoggerService, andUserService - Attempts to retrieve user claims via
userService.getClaims() - If claims are null (user is not authenticated), allows navigation
- If claims exist, redirects to the home page (
/) - Handles exceptions by logging errors and displaying alerts
Usage
Real-World Examples
ThesignedOutGuard is used in Jet to protect:
/reset-password- Password reset page/sign-up- User registration page
Source Code
unsavedChangesGuard
Prevents navigation away from a component with unsaved changes by prompting the user for confirmation.Type Signature
Parameters
The component being deactivated, must implement the
CanComponentDeactivate interface with a hasUnsavedChanges() methodThe activated route snapshot (unused)
The current router state (unused)
The next router state, used to check the destination URL
Return Value
ReturnsGuardResult:
trueif navigation is allowed (no unsaved changes, or user confirms navigation, or navigating to sign-in)falseif the user cancels the confirmation dialog
Behavior
- If navigating to
/sign-in, allows navigation without checking for unsaved changes - Calls
component.hasUnsavedChanges()to check if there are unsaved changes - If there are unsaved changes, displays a browser confirmation dialog with a translated message
- Returns the result of the user’s choice
Component Interface
Components using this guard must implement theCanComponentDeactivate interface:
Usage
Real-World Examples
TheunsavedChangesGuard is used in Jet to protect:
/profile- Prevents losing unsaved profile changes/update-password- Prevents losing password form data
Source Code
Dependencies
All guards use Angular’s functional guard approach and inject dependencies using theinject() function:
Router- For creating redirect URL treesAlertService- For displaying error messages to usersLoggerService- For logging errors and exceptionsUserService- For retrieving user authentication claimsTranslocoService- For internationalized confirmation messages (unsavedChangesGuard only)