Hone logo
Hone
Problems

Secure Your Vue Routes: Implementing Authentication and Authorization Guards

In modern web applications, protecting certain routes from unauthorized access is crucial for both security and user experience. This challenge focuses on building robust route guards in a Vue.js application using TypeScript. You will learn to implement guards that check user authentication status and enforce authorization rules before allowing navigation to specific pages.

Problem Description

Your task is to create two distinct route guards for a Vue.js application:

  1. Authentication Guard: This guard will prevent unauthenticated users from accessing protected routes. If a user is not logged in and attempts to navigate to a protected route, they should be redirected to a login page.
  2. Authorization Guard: This guard will control access to routes based on user roles or specific permissions. If a user attempts to access a route for which they do not have the necessary authorization, they should be presented with an "Access Denied" message or redirected to a fallback page.

Key Requirements:

  • Use Vue Router's navigation guards.
  • Implement guards using TypeScript for type safety.
  • Simulate authentication status and user roles.
  • Handle redirection logic gracefully.

Expected Behavior:

  • Authentication Guard:
    • If isAuthenticated is true, allow navigation to the route.
    • If isAuthenticated is false, redirect to /login.
  • Authorization Guard:
    • If the user's role matches the requiredRole for the route, allow navigation.
    • If the user's role does not match, redirect to /access-denied or display an alert.

Edge Cases to Consider:

  • What happens if the user is already on the login page and tries to access it again while authenticated?
  • How do you handle scenarios where route metadata for authorization might be missing?
  • Ensure redirects do not create infinite loops.

Examples

Example 1: Authentication Guard - Accessing Protected Route while Unauthenticated

  • Scenario: A user attempts to navigate to /dashboard. The isAuthenticated flag is false.
  • Input:
    • to.path: /dashboard
    • from.path: /home
    • isAuthenticated: false
  • Output: Navigation is prevented. The user is redirected to /login.
  • Explanation: The authentication guard detects isAuthenticated is false and triggers a redirect to the login page.

Example 2: Authentication Guard - Accessing Protected Route while Authenticated

  • Scenario: A user attempts to navigate to /profile. The isAuthenticated flag is true.
  • Input:
    • to.path: /profile
    • from.path: /home
    • isAuthenticated: true
  • Output: Navigation is allowed to /profile.
  • Explanation: The authentication guard sees isAuthenticated is true and allows the user to proceed.

Example 3: Authorization Guard - Accessing Admin Route with Admin Role

  • Scenario: A user with the role: 'admin' attempts to navigate to /admin. The route requires the admin role.
  • Input:
    • to.path: /admin
    • from.path: /dashboard
    • user.role: 'admin'
    • to.meta.requiredRole: 'admin'
  • Output: Navigation is allowed to /admin.
  • Explanation: The authorization guard checks if the user's role ('admin') matches the route's required role ('admin') and permits access.

Example 4: Authorization Guard - Accessing Admin Route without Admin Role

  • Scenario: A user with the role: 'user' attempts to navigate to /admin. The route requires the admin role.
  • Input:
    • to.path: /admin
    • from.path: /dashboard
    • user.role: 'user'
    • to.meta.requiredRole: 'admin'
  • Output: Navigation is prevented. The user is redirected to /access-denied (or an alert is shown).
  • Explanation: The authorization guard finds a mismatch between the user's role ('user') and the route's required role ('admin'), hence denying access.

Constraints

  • The application will be built using Vue 3 and Vue Router 4.
  • All code, including route guards and route definitions, must be written in TypeScript.
  • Authentication status will be simulated using a simple boolean variable (isAuthenticated).
  • User roles will be simulated using a string property on a user object (user.role).
  • Routes requiring specific roles will be defined using the meta property in Vue Router.
  • Performance is not a primary concern for this challenge; clarity and correctness are prioritized.

Notes

  • You'll need to define your Vue Router instance and its routes.
  • Consider how you will manage and access the isAuthenticated state and user object within your guards. These could be global state management variables, injected services, or simple global variables for this exercise.
  • Think about where the requiredRole metadata should be placed within your route definitions.
  • The redirection mechanism in Vue Router's beforeEach guard is essential for implementing these guards.
  • You'll need to create placeholder components for /login, /access-denied, and other example routes to test your guards.
Loading editor...
typescript