Secure Route Access with Angular's CanActivate Guard
Angular's CanActivate guard is a powerful mechanism for controlling access to routes within your application. This challenge asks you to implement a CanActivate guard that determines whether a user should be allowed to navigate to a specific route based on their authentication status. This is a fundamental concept in building secure web applications, preventing unauthorized users from accessing sensitive areas.
Problem Description
You need to create an Angular CanActivate guard called AuthGuard. This guard will determine if a user is logged in before allowing them to access a protected route. The guard should check a boolean property called isAuthenticated on a service called AuthService. If AuthService.isAuthenticated is true, the guard should allow navigation; otherwise, it should redirect the user to a login page (represented by the route /login).
Key Requirements:
- Implement
CanActivate: YourAuthGuardclass must implement theCanActivateinterface. - Dependency Injection: Inject the
AuthServiceinto yourAuthGuard. - Authentication Check: Use
AuthService.isAuthenticatedto determine access. - Redirection: If not authenticated, redirect to
/login. Use theRouterservice for redirection. - Return Value: The guard must return a
boolean(allowing access) or aRouterobject (redirecting).
Expected Behavior:
- When a user attempts to navigate to a protected route (e.g.,
/profile) whileAuthService.isAuthenticatedisfalse, theAuthGuardshould intercept the navigation and redirect the user to/login. - When a user attempts to navigate to a protected route while
AuthService.isAuthenticatedistrue, theAuthGuardshould allow navigation to proceed. - The guard should not block navigation if the user is already on the
/loginroute.
Edge Cases to Consider:
- What happens if
AuthServiceis not properly initialized orisAuthenticatedis undefined? (Handle this gracefully, perhaps defaulting to redirecting to/login). - Consider the scenario where the user navigates directly to
/login. The guard should not prevent this.
Examples
Example 1:
Input: User attempts to navigate to `/profile` and `AuthService.isAuthenticated` is `false`.
Output: User is redirected to `/login`.
Explanation: The AuthGuard detects the user is not authenticated and redirects them to the login page.
Example 2:
Input: User attempts to navigate to `/profile` and `AuthService.isAuthenticated` is `true`.
Output: User is allowed to navigate to `/profile`.
Explanation: The AuthGuard detects the user is authenticated and allows navigation to proceed.
Example 3:
Input: User attempts to navigate to `/login` and `AuthService.isAuthenticated` is `false`.
Output: User is allowed to navigate to `/login`.
Explanation: The AuthGuard does not block navigation to the login page.
Constraints
- The
AuthServiceis assumed to be available and properly injected. - The
/loginroute is assumed to exist. - The solution must be written in TypeScript and compatible with Angular.
- The solution should be concise and readable.
Notes
- You'll need to import the necessary Angular modules:
ActivatedRouteSnapshot,RouterStateSnapshot,Router, andCanActivate. - Consider using the
Router.navigateByUrl()method for redirection. - Think about how to handle potential errors or undefined values in
AuthService.isAuthenticated. A default redirect is a good approach. - This challenge focuses on the core logic of the
CanActivateguard. Error handling and more complex authentication flows are beyond the scope of this exercise.