@vaadin/hilla-react-auth
Version:
Hilla auth utils for React
38 lines (37 loc) • 1.59 kB
TypeScript
import { type IndexRouteObject, type NonIndexRouteObject } from "react-router";
import { type AccessProps } from "./useAuth.js";
type CustomMetadata = Record<string, any>;
type HandleWithAuth = Readonly<{
handle?: AccessProps & CustomMetadata
}>;
type Override<
T,
E
> = E & Omit<T, keyof E>;
type IndexRouteObjectWithAuth = Override<IndexRouteObject, HandleWithAuth>;
type NonIndexRouteObjectWithAuth = Override<Override<NonIndexRouteObject, HandleWithAuth>, {
children?: RouteObjectWithAuth[]
}>;
export type RouteObjectWithAuth = IndexRouteObjectWithAuth | NonIndexRouteObjectWithAuth;
/**
* Adds protection to a single route that requires authentication.
* These route should contain the {@link AccessProps.loginRequired} and/or
* {@link AccessProps.rolesAllowed} property to get the protection. Route
* without that property won't be protected.
*
* @param route - the route to protect
* @param redirectPath - the path to redirect to if the route is protected
* and the user is not authenticated.
* @returns the route extended with protection if needed
*/
export declare function protectRoute(route: RouteObjectWithAuth, redirectPath?: string): RouteObjectWithAuth;
/**
* Protects a route tree with {@link protectRoute} function.
*
* @param routes - the roots of the route tree that requires protection.
* @param redirectPath - the path to redirect to if the route is
* protected and the user is not authenticated.
* @returns the protected route tree
*/
export declare function protectRoutes(routes: RouteObjectWithAuth[], redirectPath?: string): RouteObjectWithAuth[];
export {};