Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Root Layout and Protected Routes #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

manuelbento19
Copy link

To ensure that only authenticated users can access the Home route, I had to implement conditional redirection logic based on the user's authentication state. A common approach to doing this in React applications is to create a protected route component. What did I do?

  • I created a protected route component (ProtectedRoute):
    This component checks whether the user is authenticated. If not, it redirects to the login page. If so, it allows access to the desired route.

Explanation

  • ProtectedRoute: This component checks whether the user is authenticated. If not, redirect to the login page using the React Router Navigate component. If the user is authenticated, it renders the Outlet component, which allows it to render the child components of the protected route.
  • Use of ProtectedRoute: We wrap the Home route with the ProtectedRoute component. This way, we guarantee that the Home route will only be accessible if the user is authenticated.

Final considerations

  • RootLayout: RootLayout was also created to standardize the layout of routes, as well as the standard navigation logic. Authentication and redirection logic has been moved to the ProtectedRoute component.

With these changes, we can ensure that the Home page will only be accessible to authenticated users and that unauthenticated users will be redirected to the login page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant