Angular CanActivate Guard: Securing User Routes
This challenge focuses on implementing a fundamental Angular security mechanism: the CanActivate guard. You will create a guard that controls access to specific routes based on a user's authentication status, ensuring that protected content is only accessible to logged-in users.
Problem Description
Your task is to implement a CanActivate guard in an Angular application. This guard will be responsible for determining whether a user is allowed to navigate to a particular route. Specifically, you need to:
- Create a
AuthGuardservice. This service should implement theCanActivateinterface. - Implement the
canActivatemethod. This method will check if the user is authenticated. - Simulate authentication. For this challenge, you will create a simple
AuthServiceto simulate checking user authentication status (e.g., returningtruefor authenticated,falsefor unauthenticated). - Configure the route. You will then apply your
AuthGuardto a specific route within your Angular application's routing configuration. - Handle unauthorized access. When
canActivatereturnsfalse, the user should be redirected to a login page.
Key Requirements:
- The
AuthGuardmust implement theCanActivateinterface from@angular/router. - The
canActivatemethod should return a boolean or aUrlTree(for redirection). - A separate
AuthServiceis required to manage the authentication state. - The guard should be applied to a hypothetical "Dashboard" route.
- If not authenticated, the user should be redirected to a "Login" route.
Expected Behavior:
- If a user is authenticated and tries to access the "Dashboard" route, they should be allowed to proceed.
- If a user is not authenticated and tries to access the "Dashboard" route, they should be redirected to the "Login" route.
Edge Cases:
- Consider what happens if the
AuthServiceis not correctly injected or if its methods return unexpected values. (Though for this simplified challenge, we assume a well-behavedAuthService).
Examples
Example 1:
Scenario: User is authenticated.
Input:
- A user's session indicates they are logged in.
- The user attempts to navigate to
/dashboard.
Output:
- The user is successfully navigated to the
/dashboardroute. - The
canActivatemethod ofAuthGuardreturnstrue.
Explanation: The AuthService reports the user as authenticated, so the canActivate method allows access to the dashboard.
Example 2:
Scenario: User is not authenticated.
Input:
- A user's session indicates they are not logged in.
- The user attempts to navigate to
/dashboard.
Output:
- The user is redirected to the
/loginroute. - The
canActivatemethod ofAuthGuardreturns aUrlTreepointing to/login.
Explanation: The AuthService reports the user as unauthenticated. The canActivate method intercepts the navigation and redirects the user to the login page.
Constraints
- Your solution must be written in TypeScript.
- You should use Angular's dependency injection system for services.
- The
AuthServiceshould be a simple injectable service. - The routing configuration should be a standard Angular
Routesarray.
Notes
- You will need to create a basic Angular project setup (or assume one exists) with a router configured.
- Focus on the implementation of the
AuthGuardand its integration with the routing. - Consider how you would typically manage authentication state in a real application (though for this challenge, a simplified
AuthServiceis sufficient). - Hint: The
Routerservice will be crucial for programmatic navigation in case of denied access.