Skip to content

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.

Terminal window
npm install oidc-js-svelte
<script lang="ts">
import { RequireAuth } from "oidc-js-svelte";
</script>
<RequireAuth>
{#snippet children()}
<h1>Protected Content</h1>
{/snippet}
{#snippet fallback()}
<p>Loading...</p>
{/snippet}
</RequireAuth>
PropTypeDefaultDescription
childrenSnippetrequiredContent to render when authenticated
fallbackSnippet-Shown while loading or redirecting
autoRefreshbooleantrueAuto-refresh expired tokens before redirecting to login
loginOptionsLoginOptions-Options passed to actions.login() when redirecting

When RequireAuth renders:

  1. If still loading (discovery, callback), render fallback
  2. If authenticated with a valid token, render children
  3. If the token is expired and autoRefresh is true, attempt a silent refresh
  4. If refresh succeeds, render children with the new token
  5. If refresh fails (or no refresh token), redirect to the IdP login
<!-- src/routes/dashboard/+page.svelte -->
<script lang="ts">
import { RequireAuth } from "oidc-js-svelte";
</script>
<RequireAuth>
{#snippet children()}
<h1>Dashboard</h1>
<p>This content is only visible when authenticated.</p>
{/snippet}
{#snippet fallback()}
<p>Checking authentication...</p>
{/snippet}
</RequireAuth>