feat: add route guard functionality for access control#557
Open
ice-rider wants to merge 2 commits intosolidjs:mainfrom
Open
feat: add route guard functionality for access control#557ice-rider wants to merge 2 commits intosolidjs:mainfrom
ice-rider wants to merge 2 commits intosolidjs:mainfrom
Conversation
Implements route guards to control navigation access based on custom conditions, similar to other popular routers. Guards can be defined per route and support both synchronous and asynchronous checks with redirect capabilities. - Add `guard` prop to RouteDefinition and Route components accepting boolean or async function - Implement `checkRouteGuard` in router context to evaluate all guards in a match chain - Create `useRouteGuard` hook for component-level guard access with pending state - Export `evaluateRouteGuard` and `normalizeGuardResult` utilities for custom implementations - Integrate guard checks with route matching to prevent rendering unauthorized routes - Add guard evaluation during preload to skip loading protected routes - Support redirect responses by automatically navigating when guard returns a string path This maintains Solid's reactive patterns while providing familiar protection mechanisms for authentication flows, feature flags, and role-based access control.
🦋 Changeset detectedLatest commit: 0cc6e4c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR introduces route guards to the Solid Router, allowing developers to control access to routes based on custom conditions such as authentication, authorization, feature flags, or any asynchronous logic. It provides a familiar API similar to other popular routers while staying true to Solid's reactive and performant principles.
Motivation
Many applications require protecting certain routes from unauthorized access or redirecting users based on their state. Previously, this had to be implemented manually inside route components or with wrapper components, leading to boilerplate and inconsistent behavior. By integrating guards directly into the router, we enable a declarative and centralized way to manage route protection.
Key Features
guardprop to route definitions (both in<Route>component and route objects) that accepts a boolean, a synchronous function, or an asynchronous function returning aRouteGuardResult.true(allow),false(deny, staying on current route), or astring(redirect path). Also supports{ allowed: boolean; redirect?: string }for more complex cases.replace: true(avoiding history stack pollution).useRouteGuardhook allows components to reactively access guard status and pending state.evaluateRouteGuardandnormalizeGuardResultfor custom implementations.Implementation Details
guardfield toRouteDefinitionandRouteDescriptiontypes.RouterContextwithcheckRouteGuardmethod that iterates through route matches (from root to leaf) and stops at the first failing guard.components.tsxto conditionally render route content.routing.tsto respect guards.Example Usage
Testing
evaluateRouteGuardandnormalizeGuardResult.Potential Impact
This change is backward-compatible: existing routes without a
guardprop behave exactly as before. No breaking changes are introduced.Let me know if any adjustments are needed. Thank you for considering this contribution!