Skip to content

useAuth

useAuth is the primary composable for interacting with the OIDC authentication state. It must be called within a component tree where oidcPlugin is installed.

Terminal window
npm install oidc-js-vue
<script setup lang="ts">
import { useAuth } from "oidc-js-vue";
const { user, isAuthenticated, isLoading, error, tokens, actions } = useAuth();
</script>
<template>
<div v-if="isLoading">Loading...</div>
<div v-else-if="error">Error: {{ error.message }}</div>
<div v-else-if="isAuthenticated">
<p>Hello, {{ user?.claims.sub }}</p>
<button @click="actions.logout()">Log out</button>
</div>
<div v-else>
<button @click="actions.login()">Log in</button>
</div>
</template>

All state properties are Vue ComputedRef values that update reactively when the auth state changes.

PropertyTypeDescription
userComputedRef<AuthUser | null>The authenticated user (claims + profile)
isAuthenticatedComputedRef<boolean>Whether the user is logged in
isLoadingComputedRef<boolean>Whether initialization is in progress
errorComputedRef<Error | null>The most recent auth error
tokensComputedRef<AuthTokens>Current access, id, and refresh tokens
actionsAuthActionsMethods for login, logout, refresh, fetchProfile
configOidcConfigThe OIDC configuration
ActionDescription
login(options?)Redirect to IdP login. Options: returnTo, extraParams
logout()Clear state and redirect to IdP logout endpoint
refresh()Exchange refresh token for new tokens
fetchProfile()Fetch the UserInfo endpoint and update user.profile