AuthRoute, RoleGuard, and client-side RBAC: what the frontend protects, and what it does not guarantee
PrivateRoute and role guards, simple checks versus CASL, and why none of this is security without the server.
On the client you often need to limit what a user can do: by role, or by a set of permissions on entities and operations. Two patterns cover most of it:
- AuthRoute / PrivateRoute — a whole page or a branch of the router is available only after authentication (sometimes with a redirect to login).
- RoleGuard / PermissionGuard — conditional rendering of a piece of UI: a button, a menu, a block, based on a role or a permission.
Server first, client second
The source of truth is the backend. Any check on the frontend is about convenience and a consistent interface: the user does not see controls they cannot use, and they do not hit an obvious 403 everywhere. The client can always be bypassed (another request, DevTools, an old build). So authorization and permission checks on operations have to live on the API. The frontend only reflects what the server already allows or rejects.
A simple app: roles are enough
If the model is flat (a few roles, no “object × action” matrix), a guard over a list of allowed roles is enough:
type RoleGuardProps = {
allowed: string[];
fallback?: React.ReactNode;
children: React.ReactNode;
};
function RoleGuard({ allowed, fallback = null, children }: RoleGuardProps) {
const auth = useAuth();
const isAllowed = checkUserRoles(allowed, auth.roles);
if (isAllowed) return <>{children}</>;
return <>{fallback}</>;
}
// Usage
<RoleGuard allowed={[Roles.Admin]}>
<Button>Delete</Button>
</RoleGuard>;
checkUserRoles is your own matching function (set intersection, a hierarchy where “admin includes editor”, and so on).
Protecting a route
AuthRoute (or PrivateRoute) solves a different problem: do not render the screen without a session (and, if you want, without the required role).
type AuthRouteProps = {
children: React.ReactNode;
};
function AuthRoute({ children }: AuthRouteProps) {
const auth = useAuth();
if (auth.isAuthorized) return <>{children}</>;
// Options: redirect to login, a “no access” page, or null.
// In react-router this is often `<Navigate to="/login" replace />`.
// In TanStack Router it is `navigate({ to: '/login' })` or `redirect` in `beforeLoad`.
return null;
}
Together with the router, this component wraps the “private” pages. For roles, people often add a separate RoleRoute, or pass allowed into a shared guard at the route level, so the logic is not copied into every page.
A richer model: RBAC and CASL
When the rules look like “who can do what to which object” (not only “role X”, but attributes of the record), it is better to write the rules in one place. RBAC (roles plus permissions, sometimes with field-level limits) is covered well in external guides, for example this RBAC overview.
On the client, declarative rules are often handled with CASL. It has integrations for React, Vue, and Angular. The idea: one ability object, and components call ability.can(action, subject) instead of scattered if (role === …) checks.
A shortened example from the docs:
import { createMongoAbility, AbilityBuilder } from "@casl/ability";
const { can, build } = new AbilityBuilder(createMongoAbility);
can("read", ["Post", "Comment"]);
can("manage", "Post", { author: "me" });
can("create", "Comment");
const ability = build();
ability.can("read", "Post"); // true
In practice you build the ability after login, from the API response (roles and permissions), so the client does not drift away from the server. The server still checks the same actions on every request.
In short
| Approach | When |
|---|---|
Role in RoleGuard | Few roles, similar screens |
AuthRoute | Authentication is required for whole pages |
| CASL or similar | Many actions and entity types, one permissions model |
Client guards improve UX and maintainability. Data security only comes from checks on the backend.