Skip to main content

API reference

The public surface of

@thunderid/nuxt, as documented in its reference pages.

hookuseThunderID()

The `useThunderID` composable provides access to the ThunderID authentication context in Nuxt applications. It exposes reactive auth state and SSR-safe methods for signing in, signing out, and signing up. This composable is auto-imported and available in every component, page, and composable without an explicit import.

Returns
isSignedIn
ComputedRef

Whether the user is currently authenticated

isLoading
ComputedRef

Whether an auth operation is in progress

isInitialized
ComputedRef

Whether the SDK has finished initializing

user
ComputedRef<User \

The authenticated user object, or `null` if not signed in

organization
ComputedRef<Organization \

The current organization context, or `null`

signIn
(...args) => Promise

Initiate the sign-in flow

signOut
() => Promise

Sign out the current user

signUp
(...args) => Promise

Initiate the sign-up flow

getAccessToken
() => Promise

Get a client-safe access token via `/api/auth/token`

Example
<script setup lang="ts">
const { isSignedIn, user, signIn, signOut } = useThunderID()
</script>

<template>
  <div>
    <p v-if="isSignedIn">Welcome, {{ user?.displayName }}!</p>
    <button v-if="isSignedIn" @click="signOut()">Sign Out</button>
    <button v-else @click="signIn()">Sign In</button>
  </div>
</template>
hookuseUser()

The `useUser` composable returns the currently authenticated user object as a reactive computed ref. It is auto-imported and available everywhere without an explicit import.

Example
<script setup lang="ts">
const user = useUser()
</script>

<template>
  <div v-if="user">
    <h1>{{ user.displayName }}</h1>
    <p>{{ user.email }}</p>
  </div>
  <p v-else>Not signed in.</p>
</template>
component<ChangeCredential />

The `ChangeCredential` component renders a form that lets a signed-in user set a new value for one of their own credentials. It collects only a new value and its confirmation, checks it against the applicable rules as the user types, and posts the change through the Nuxt server route to ThunderID. It is auto-registered by the Nuxt module. It reads the rules it needs from the user type schema already resolved by `ThunderIDRoot` (`GET /users/me/meta`), so it adds no network request beyond the write itself. Every default label, placeholder, and message is built from the credential attribute's own `displayName` in that schema, so it always matches whatever an admin named it there. By default it manages the `password` credential. To manage a different one, for example a PIN declared on the user type schema, set `attribute`; render the component once per credential to let a user manage more than one. :::note `ChangeCredential` collects only a new value and its confirmation, not the account's existing value. The self-service credential write path does not verify the current value today, so asking for one would only teach the user a false sense of security. Once server-side current-value verification ships, that field returns without a breaking change to this component's public props. :::

Props
attributeoptional
string

The credential attribute this instance manages, any attribute the user type schema declares `credential: true` for. Defaults to `'password'`.

cardLayoutoptional
boolean

Whether to wrap the form in a bordered card. Defaults to `false`.

classNameoptional
string

Additional CSS class added to the root element. Defaults to `''`.

policyoptional
PasswordPolicy

The rules the new value must satisfy. Defaults to the applicable rules from the user type schema.

preferencesoptional
Preferences

Component-level preference overrides, including i18n.

showRequirementsoptional
boolean

Whether to render the live requirement checklist. Defaults to `true`.

titleoptional
string

Overrides the default `"Change {credential}"` heading. Pass an empty string to omit the heading entirely. Defaults to a translated, schema-derived heading.

Example
<template>
  <div>
    <h2>Security</h2>
    <ChangeCredential />
  </div>
</template>
component<Loading />

The `Loading` component renders its default slot while the authentication state is being resolved. It is useful for showing skeleton screens or spinners during the initial hydration phase.

Example
<template>
  <Loading>
    <p>Loading...</p>
  </Loading>

  <SignedIn>
    <p>Welcome back!</p>
  </SignedIn>

  <SignedOut>
    <SignInButton />
  </SignedOut>
</template>
component<SignInButton />

The `SignInButton` component initiates the sign-in flow when clicked. It is auto-registered by the Nuxt module and requires no imports.

Props
preferencesoptional
Preferences

Customization options for i18n and theming

Example
<template>
  <SignInButton />
</template>
component<SignOutButton />

The `SignOutButton` component signs out the current user when clicked. It is auto-registered by the Nuxt module and requires no imports.

Props
preferencesoptional
Preferences

Customization options for i18n and theming

Example
<template>
  <SignOutButton />
</template>
component<SignUpButton />

The `SignUpButton` component initiates the sign-up flow when clicked. It is auto-registered by the Nuxt module and requires no imports.

Props
preferencesoptional
Preferences

Customization options for i18n and theming

Example
<template>
  <SignUpButton />
</template>
component<SignedIn />

The `SignedIn` component renders its default slot only when the user is authenticated. It is the Vue equivalent of the React `` component and is auto-registered by the Nuxt module.

Example
<template>
  <SignedIn>
    <p>Welcome! You are signed in.</p>
  </SignedIn>
</template>
component<SignedOut />

The `SignedOut` component renders its default slot only when the user is **not** authenticated. It is auto-registered by the Nuxt module.

Example
<template>
  <SignedOut>
    <SignInButton />
  </SignedOut>
</template>
component<ThunderIDRoot />

The `ThunderIDRoot` component is the root provider for the ThunderID Nuxt SDK. It mounts the full provider tree, including i18n, branding, theme, flow, user, and organization, and must wrap all content that uses ThunderID composables or components.

Example
<template>
  <ThunderIDRoot>
    <NuxtPage />
  </ThunderIDRoot>
</template>
component<User />

The `User` component provides access to the authenticated user object via a scoped slot. It is auto-registered by the Nuxt module.

Example
<template>
  <User>
    <template #default="{ user }">
      <div v-if="user">
        <h1>Welcome, {{ user.displayName }}!</h1>
        <p>{{ user.email }}</p>
      </div>
    </template>
  </User>
</template>
component<UserDropdown />

The `UserDropdown` component renders a button showing the user's avatar or name. When clicked, it opens a dropdown menu with links to the user profile and a sign-out action. It is auto-registered by the Nuxt module.

Props
appearanceoptional
Appearance

Customize the component's visual appearance

preferencesoptional
Preferences

Customization options for i18n and theming

Example
<template>
  <nav>
    <SignedIn>
      <UserDropdown />
    </SignedIn>
    <SignedOut>
      <SignInButton />
    </SignedOut>
  </nav>
</template>
component<UserProfile />

The `UserProfile` component renders a full profile management interface. Users can view and edit their personal information, change their password, and manage linked accounts. It is auto-registered by the Nuxt module.

Props
appearanceoptional
Appearance

Customize the component's visual appearance

sectionsoptional
string[]

Restrict which profile sections are shown. Defaults to all sections

preferencesoptional
Preferences

Customization options for i18n and theming

Example
<template>
  <UserProfile />
</template>
middlewaredefineThunderIDMiddleware()

`defineThunderIDMiddleware` is a factory function that returns a Nuxt route middleware. It handles authentication guards and scope checks, and redirects unauthenticated users. The module also registers a named `'auth'` middleware that you can apply to any page using `definePageMeta`. It uses `defineThunderIDMiddleware` with its default options.

Options
redirectTo
string

Where to redirect unauthenticated (or unauthorized) users

requireScopes
string[]

One or more OIDC scopes that must be present in the user's session

Example
<script setup lang="ts">
definePageMeta({ middleware: 'auth' })
</script>

<template>
  <h1>Dashboard</h1>
</template>
functioncreateRouteMatcher()

`createRouteMatcher` takes an array of route patterns and returns a predicate function. Call the predicate with a path string to check whether it matches any of the patterns. It is primarily used in global middleware to apply auth guards to a set of routes without adding `definePageMeta` to every page.

Example
import { defineThunderIDMiddleware } from '@thunderid/nuxt'
import { createRouteMatcher } from '@thunderid/nuxt/utils'

const isProtected = createRouteMatcher([
  '/dashboard',
  '/dashboard/**',
  '/account/**',
  '/settings/**',
])

export default defineNuxtRouteMiddleware((to) => {
  if (isProtected(to.path)) {
    return defineThunderIDMiddleware()
  }
})
exportModule Configuration

The `@thunderid/nuxt` module is configured via the `thunderid` key in `nuxt.config.ts`. The configuration covers credentials, redirect URLs, session handling, and UI preferences.

Example
export default defineNuxtConfig({
  modules: ['@thunderid/nuxt'],
  thunderid: {
    clientId: '<your-client-id>',
    clientSecret: '<your-client-secret>',
    baseUrl: 'https://localhost:8090',
    afterSignInUrl: '/dashboard',
    afterSignOutUrl: '/',
    scopes: ['openid', 'profile', 'internal_login'],
    preferences: {
      theme: {
        inheritFromBranding: true,
        mode: 'system',
      },
    },
  },
})
exportThunderIDError

`ThunderIDError` is the structured error class thrown by the ThunderID Nuxt SDK. It extends `Error` and carries a typed `ErrorCode`, an optional HTTP status code, an optional cause, and optional context metadata.

Example
import { ThunderIDError, ErrorCode } from '@thunderid/nuxt/errors'
import { getValidAccessToken } from '@thunderid/nuxt/server'

export default defineEventHandler(async (event) => {
  try {
    const token = await getValidAccessToken(event)
    return await $fetch('/api/resource', {
      headers: { Authorization: `Bearer ${token}` },
    })
  } catch (err) {
    if (err instanceof ThunderIDError) {
      if (err.code === ErrorCode.SessionExpired) {
        throw createError({ statusCode: 401, statusMessage: 'Session expired' })
      }
      if (err.code === ErrorCode.TokenRefreshFailed) {
        throw createError({ statusCode: 401, statusMessage: 'Could not refresh token' })
      }
    }
    throw err
  }
})
exportgetThunderIDContext()

`getThunderIDContext` returns the ThunderID context object that the SDK's server plugin attaches to each H3 event during SSR. This gives synchronous access to the session and pre-fetched SSR data without an additional async call.

Example
import { getThunderIDContext } from '@thunderid/nuxt/server'

export default defineEventHandler((event) => {
  const ctx = getThunderIDContext(event)

  return {
    isSignedIn: ctx?.isSignedIn ?? false,
    sub: ctx?.session?.sub ?? null,
  }
})
exportgetValidAccessToken()

`getValidAccessToken` returns a valid access token for the current session. If the stored access token is expired or about to expire, it automatically uses the refresh token to obtain a new one and updates the session cookie.

Example
import { getValidAccessToken } from '@thunderid/nuxt/server'

export default defineEventHandler(async (event) => {
  const accessToken = await getValidAccessToken(event)

  const result = await $fetch('https://api.example.com/data', {
    headers: { Authorization: `Bearer ${accessToken}` },
  })

  return result
})
exportrequireServerSession()

`requireServerSession` reads the ThunderID session from the current H3 event and returns the decoded `ThunderIDSessionPayload`. If no valid session exists, it throws an H3 error with status code `401`.

Example
import { requireServerSession } from '@thunderid/nuxt/server'

export default defineEventHandler(async (event) => {
  const session = await requireServerSession(event)
  // session is guaranteed to be defined here
  return { sub: session.sub }
})
exportuseServerSession()

`useServerSession` reads the ThunderID session from the current H3 event and returns the decoded `ThunderIDSessionPayload`, or `null` if no valid session exists.

Example
import { useServerSession } from '@thunderid/nuxt/server'

export default defineEventHandler(async (event) => {
  const session = await useServerSession(event)

  if (!session) {
    throw createError({ statusCode: 401, statusMessage: 'Unauthorized' })
  }

  return { sub: session.sub, scopes: session.scopes }
})
ThunderID LogoThunderID Logo

Product

DocsAPIsSDKs
© Copyright Linux Foundation Europe.For web site terms of use, trademark policy and other project policies please see https://linuxfoundation.eu/en/policies.