Hone logo
Hone
Problems

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 AuthGuard service. This service should implement the CanActivate interface.
  • Implement the canActivate method. This method will check if the user is authenticated.
  • Simulate authentication. For this challenge, you will create a simple AuthService to simulate checking user authentication status (e.g., returning true for authenticated, false for unauthenticated).
  • Configure the route. You will then apply your AuthGuard to a specific route within your Angular application's routing configuration.
  • Handle unauthorized access. When canActivate returns false, the user should be redirected to a login page.

Key Requirements:

  • The AuthGuard must implement the CanActivate interface from @angular/router.
  • The canActivate method should return a boolean or a UrlTree (for redirection).
  • A separate AuthService is 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 AuthService is not correctly injected or if its methods return unexpected values. (Though for this simplified challenge, we assume a well-behaved AuthService).

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 /dashboard route.
  • The canActivate method of AuthGuard returns true.

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 /login route.
  • The canActivate method of AuthGuard returns a UrlTree pointing 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 AuthService should be a simple injectable service.
  • The routing configuration should be a standard Angular Routes array.

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 AuthGuard and its integration with the routing.
  • Consider how you would typically manage authentication state in a real application (though for this challenge, a simplified AuthService is sufficient).
  • Hint: The Router service will be crucial for programmatic navigation in case of denied access.
Loading editor...
typescript