{"record":{"id":"04bc816758cce1da","repo":"hcengineering/platform","slug":"no-token-available-04bc81","errorCode":null,"errorMessage":"No token available","messagePattern":"No token available","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"plugins/export-resources/src/components/ExportButton.svelte","lineNumber":33,"sourceCode":"<script lang=\"ts\">\n  import { Class, Doc, Ref } from '@hcengineering/core'\n  import { Button, showPopup } from '@hcengineering/ui'\n  import { getMetadata } from '@hcengineering/platform'\n  import presentation, { MessageBox } from '@hcengineering/presentation'\n  import { type TransformConfig } from '@hcengineering/export'\n  import plugin from '../plugin'\n\n  export let _class: Ref<Class<Doc>>\n  export let query: string = ''\n  export let visible: boolean = false\n  export let config: TransformConfig = {}\n\n  async function handleExport (): Promise<void> {\n    try {\n      const baseUrl = getMetadata(plugin.metadata.ExportUrl)\n      const token = getMetadata(presentation.metadata.Token)\n      if (token == null) {\n        throw new Error('No token available')\n      }\n\n      const res = await fetch(`${baseUrl}/exportSync?format=csv`, {\n        method: 'POST',\n        headers: {\n          Authorization: `Bearer ${token}`,\n          'Content-Type': 'application/json'\n        },\n        body: JSON.stringify({\n          _class,\n          query,\n          attributesOnly: true,\n          config\n        })\n      })\n\n      if (!res.ok) {\n        showPopup(MessageBox, {","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/plugins/export-resources/src/components/ExportButton.svelte#L15-L51","documentation":"ExportButton reads the auth token from presentation metadata (`presentation.metadata.Token`). If it is null or undefined the export cannot authenticate against the export service, so 'No token available' is thrown before any fetch is made. It indicates the user's session/token was not injected into the client metadata.","triggerScenarios":"`getMetadata(presentation.metadata.Token)` returns null because the login/session flow did not provide a token, or the plugin metadata was not registered/initialized.","commonSituations":"Expired or missing session, running the component in a context without login metadata (tests, storybook, embedded preview), or misconfigured auth plugin registration.","solutions":["Ensure the user is logged in and the token metadata is set before rendering the export button.","Check that the presentation/auth plugin providing metadata.Token is properly installed and initialized.","Re-authenticate (refresh session) if the token was dropped after expiry, then retry the export."],"exampleFix":"// before\nconst token = getMetadata(presentation.metadata.Token)\nif (token == null) throw new Error('No token available')\n// after\nconst token = getMetadata(presentation.metadata.Token)\nif (token == null) {\n  ui.notify('Please sign in again to export')\n  void login()\n  return\n}","handlingStrategy":"type-guard","validationCode":"const token = getMetadata(presentation.metadata.Token)\nif (token == null || token === '') { ui.notify('Sign in to export'); return }","typeGuard":"function hasToken(t: string | null | undefined): t is string { return typeof t === 'string' && t.length > 0 }","tryCatchPattern":"try {\n  await handleExport()\n} catch (e) {\n  if (e instanceof Error && e.message === 'No token available') {\n    ui.notify('Your session has no auth token; please sign in again')\n  } else throw e\n}","preventionTips":["Gate export UI behind an authenticated session check.","Refresh tokens before expiry so metadata.Token is always populated.","Verify auth/presentation plugin metadata registration in embedded or test environments."],"tags":["authentication","token","metadata"],"backgroundTag":"missing-auth-token","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}