Skip to content

RequireAuth

RequireAuth is a renderless component that only renders its default slot when the user is authenticated. The package also provides createAuthGuard for protecting Vue Router routes.

Terminal window
npm install oidc-js-vue
<script setup lang="ts">
import { RequireAuth } from "oidc-js-vue";
</script>
<template>
<RequireAuth>
<template #fallback>
<p>Loading...</p>
</template>
<div>Protected content</div>
</RequireAuth>
</template>
PropTypeDefaultDescription
autoRefreshbooleantrueAuto-refresh expired tokens before redirecting to login
loginOptionsLoginOptions-Options passed to actions.login() when redirecting
SlotDescription
defaultContent rendered when the user is authenticated
fallbackContent rendered while loading or when not authenticated

createAuthGuard creates a Vue Router beforeEach guard that protects routes requiring authentication. Call it inside <script setup>.

<script setup lang="ts">
import { useRouter } from "vue-router";
import { createAuthGuard } from "oidc-js-vue";
const router = useRouter();
createAuthGuard(router);
</script>
  1. If auth is still loading, waits for initialization to complete
  2. If authenticated with a valid token, allows navigation
  3. If the token is expired, attempts a silent refresh
  4. If refresh fails or no refresh token exists, redirects to IdP login

The guard preserves the target route path as returnTo, so the user returns to the requested page after login.

OptionTypeDefaultDescription
loginOptionsLoginOptions-Options passed to actions.login() when redirecting