Hone logo
Hone
Problems

Implementing a beforeEach Guard for Route Navigation in Vue.js (TypeScript)

This challenge focuses on implementing a beforeEach navigation guard in a Vue.js application using TypeScript. Navigation guards allow you to intercept navigation requests, potentially redirecting to a different location or preventing the navigation altogether, based on certain conditions (e.g., authentication status). This is crucial for controlling access to different parts of your application and ensuring a secure user experience.

Problem Description

You are tasked with creating a Vue.js component that utilizes a beforeEach navigation guard to check if a user is authenticated before allowing them to navigate to a protected route. The application has a simple structure: a home route (/) and a protected route (/profile). The beforeEach guard should redirect unauthenticated users to the home route. Assume you have a function isAuthenticated() that returns a boolean indicating whether the user is currently logged in. The component should also display a message indicating the current authentication status.

Key Requirements:

  • Implement a beforeEach navigation guard within your Vue Router.
  • The guard should check the result of the isAuthenticated() function.
  • If isAuthenticated() returns false, redirect the user to the home route (/).
  • If isAuthenticated() returns true, allow navigation to the intended route.
  • The component should display a message indicating whether the user is authenticated or not.
  • The solution must be written in TypeScript.

Expected Behavior:

  1. Unauthenticated User: When an unauthenticated user attempts to navigate to /profile, they should be redirected to /. The component should display a message like "Not authenticated. Redirecting to home."
  2. Authenticated User: When an authenticated user attempts to navigate to /profile, they should be allowed to navigate to /profile. The component should display a message like "Authenticated. Proceeding to profile."
  3. Initial Load: On initial load (e.g., visiting / or /profile directly), the component should display the appropriate authentication status message based on the result of isAuthenticated().

Edge Cases to Consider:

  • The isAuthenticated() function might be asynchronous (e.g., fetching authentication status from a server). Handle this appropriately within the beforeEach guard. For simplicity, assume isAuthenticated() is synchronous in this challenge.
  • Consider what happens if the user navigates back and forth between the routes. The beforeEach guard should consistently enforce the authentication check.

Examples

Example 1:

Input: User is not authenticated, attempts to navigate to /profile
Output: User is redirected to /, component displays "Not authenticated. Redirecting to home."
Explanation: The beforeEach guard detects the unauthenticated state and redirects to the home route.

Example 2:

Input: User is authenticated, attempts to navigate to /profile
Output: User is allowed to navigate to /profile, component displays "Authenticated. Proceeding to profile."
Explanation: The beforeEach guard detects the authenticated state and allows navigation.

Example 3:

Input: User is not authenticated, initially loads /profile
Output: User remains on /profile briefly, then is redirected to /, component displays "Not authenticated. Redirecting to home."
Explanation: The beforeEach guard intercepts the navigation attempt and redirects.

Constraints

  • The solution must use Vue Router version 4 or higher.
  • The solution must be written in TypeScript.
  • The isAuthenticated() function is provided and assumed to be synchronous. Do not implement it.
  • The application should have at least two routes: / (home) and /profile (protected).
  • The component displaying the authentication status should be a single Vue component.

Notes

  • The beforeEach guard is a function that receives the to, from, and next arguments. The to argument represents the target route, from represents the current route, and next is a function that allows you to proceed to the next route or redirect.
  • Think about how to handle the asynchronous nature of authentication checks if they were present. While not required for this problem, it's a good consideration for real-world applications.
  • Focus on the correct placement and usage of the beforeEach guard within your Vue Router configuration.
Loading editor...
typescript