Skip to main content
Back to Nuxt

useServerSession()

useServerSession reads the ThunderID session from the current H3 event and returns the decoded ThunderIDSessionPayload, 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

Explore with AI

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.