You can protect specific routes in your documentation by adding the protectedRoutes property to
your Zudoku configuration. This requires authentication to
be configured. The property supports two formats: a simple array of path patterns, or an advanced
object format with custom authorization logic.
Client-side protection only
protectedRoutes are client-side only. Do not rely on protectedRoutes to hide sensitive
information.
We are working on an additional offering that secures this data server-side.
Array Format
The simplest way to protect routes is to provide an array of path patterns. Users must be authenticated to access these routes.
zudoku.config.ts
When a user tries to access a protected route, a login dialog will appear prompting them to sign in or register. After logging in, they are automatically redirected back to the route they were trying to access.
Object Format
For more complex authorization logic, you can provide a record mapping route patterns to custom callback functions:
zudoku.config.ts
Callback Parameters
The callback function receives an object with:
auth- The current authentication state, includingisAuthenticated,isPending,profile, and morecontext- The Zudoku context providing access to configuration and utilitiesreasonCode- An object containing the reason code constantsUNAUTHORIZEDandFORBIDDEN(see Reason Codes)
Return Values
The callback can return a boolean or a reason code string:
| Return value | Behavior |
|---|---|
true | Allow access to the route |
false | Treated as UNAUTHORIZED - prompts login |
reasonCode.UNAUTHORIZED | Show a login dialog prompting the user to sign in |
reasonCode.FORBIDDEN | Show a 403 "Access Denied" page |
Reason Codes
Reason codes allow you to distinguish between users who need to sign in and users who are signed in but lack permission. This is useful for building role-based or attribute-based access control.
UNAUTHORIZED- The user is not authenticated. A login dialog is shown, and navigation to the route is blocked until the user signs in.FORBIDDEN- The user is authenticated but does not have permission. A 403 "Access Denied" page is displayed instead of the route content.
zudoku.config.ts
Navigation Blocking
When a user navigates to a route that returns false or UNAUTHORIZED, navigation is intercepted
before the page changes. The user stays on the current page while a login dialog is displayed. If
the user cancels, they remain on the current page. If they log in successfully, navigation
automatically proceeds to the protected route.
Routes that return FORBIDDEN do not block navigation — the user navigates to the route and sees
the "Access Denied" page.
Path Patterns
The path patterns follow the same syntax as React Router:
:parammatches a URL segment up to the next/,?, or#*matches zero or more characters up to the next/,?, or#/*matches all characters after the pattern
For example:
/users/:idmatches/users/123or/users/abc/docs/*matches/docs/getting-startedor/docs/api/reference/settingsmatches only the exact path/settings
Next Steps
- Learn about authentication providers supported by Zudoku
- Configure user data display