RequireAuth
RequireAuth is a guard component that ensures its children are only rendered when the user is authenticated. It handles token expiry and automatic refresh.
Installation
Section titled “Installation”npm install oidc-js-solidimport { RequireAuth } from "oidc-js-solid";
function ProtectedPage() { return ( <RequireAuth fallback={<div>Loading...</div>}> <h1>Protected Content</h1> </RequireAuth> );}| Prop | Type | Default | Description |
|---|---|---|---|
children | JSX.Element | required | Content to render when authenticated |
fallback | JSX.Element | - | Shown while loading or redirecting |
autoRefresh | boolean | true | Auto-refresh expired tokens before redirecting to login |
loginOptions | LoginOptions | - | Options passed to actions.login() when redirecting |
Behavior
Section titled “Behavior”When RequireAuth renders:
- If still loading (discovery, callback), render
fallback - If authenticated with a valid token, render
children - If the token is expired and
autoRefreshistrue, attempt a silent refresh - If refresh succeeds, render
childrenwith the new token - If refresh fails (or no refresh token), redirect to the IdP login
With Solid Router
Section titled “With Solid Router”import { Route } from "@solidjs/router";import { RequireAuth } from "oidc-js-solid";
<Route path="/" component={Home} /><Route path="/dashboard" component={() => ( <RequireAuth fallback={<Spinner />}> <Dashboard /> </RequireAuth>)} />