import { defineEventHandler, createError } from 'h3'
import type LogtoClient from '@logto/node'

export default defineEventHandler(async (event) => {
  const client = event.context.logtoClient as LogtoClient | undefined

  if (!client) {
    throw createError({ statusCode: 401, message: 'Not authenticated' })
  }

  try {
    const claims = await client.getIdTokenClaims()
    return { claims }
  }
  catch {
    return { claims: null }
  }
})
