Skip to main content

`useServerSession()`

Cloud has the newest features. Some may not be in a release yet; the newest release is Identity & Access Management 7.3.0.

useServerSession reads the Choreo Identity & Access Management session from the current H3 event and returns the decoded <ProductName />SessionPayload, or null if no valid session exists.

Signature​

useServerSession(event: H3Event): Promise<ThunderIDSessionPayload | null>

Import​

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

Usage​

In a Nuxt API Route​

server/api/profile.get.ts
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 }
})

Server Middleware​

server/middleware/log.ts
import { useServerSession } from '@thunderid/nuxt/server'

export default defineEventHandler(async (event) => {
const session = await useServerSession(event)
if (session) {
console.log('Authenticated request from:', session.sub)
}
})

Return Value​

Returns a Promise<ThunderIDSessionPayload | null>. Returns null when:

  • No session cookie is present on the request
  • The session cookie cannot be verified (invalid signature or expired)

ThunderIDSessionPayload​

PropertyTypeDescription
substringThe user's subject identifier
sessionIdstringThe internal session ID
accessTokenstringThe current access token
accessTokenExpiresAtnumber | undefinedAccess token expiry as a Unix timestamp
refreshTokenstring | undefinedThe refresh token, if issued
idTokenstring | undefinedThe ID token, if issued
scopesstringSpace-separated list of granted scopes
organizationIdstring | undefinedThe active organization ID, if any
expnumberJWT expiry timestamp
iatnumberJWT issued-at timestamp

Notes​

Was this page helpful?