{"record":{"id":"39ab8c2240515c45","repo":"hcengineering/platform","slug":"accounts-url-or-token-is-not-defined-39ab8c","errorCode":null,"errorMessage":"Accounts URL or token is not defined","messagePattern":"Accounts URL or token is not defined","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"plugins/gmail-resources/src/api.ts","lineNumber":31,"sourceCode":"// limitations under the License.\n//\nimport { getMetadata } from '@hcengineering/platform'\nimport presentation from '@hcengineering/presentation'\nimport login from '@hcengineering/login'\nimport { type GmailSyncState, gmailIntegrationKind } from '@hcengineering/gmail'\nimport {\n  getIntegrationClient as getIntegrationClientRaw,\n  type IntegrationClient,\n  request as httpRequest\n} from '@hcengineering/integration-client'\n\nimport gmail from './plugin'\n\nexport async function getIntegrationClient (): Promise<IntegrationClient> {\n  const accountsUrl = getMetadata(login.metadata.AccountsUrl)\n  const token = getMetadata(presentation.metadata.Token)\n  if (accountsUrl === undefined || token === undefined) {\n    throw new Error('Accounts URL or token is not defined')\n  }\n  return getIntegrationClientRaw(accountsUrl, token, gmailIntegrationKind, 'gmail')\n}\n\nconst url = getMetadata(gmail.metadata.GmailURL) ?? ''\n\nasync function request (method: 'GET' | 'POST' | 'DELETE', path?: string, body?: any): Promise<any> {\n  return await httpRequest({\n    baseUrl: url,\n    method,\n    path,\n    token: getMetadata(presentation.metadata.Token),\n    body\n  })\n}\n\nexport async function getState (socialId: string): Promise<GmailSyncState | null> {\n  return await request('GET', `/state?socialId=${encodeURIComponent(socialId)}`)","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/plugins/gmail-resources/src/api.ts#L13-L49","documentation":"getIntegrationClient in gmail-resources builds an authenticated IntegrationClient for the Gmail integration. It requires both the AccountsUrl (login.metadata.AccountsUrl) and the current session token (presentation.metadata.Token) metadata; if either is undefined it throws 'Accounts URL or token is not defined'.","triggerScenarios":"Calling integrationClient/getIntegrationClient before login metadata is populated, in a deployment where login.metadata.AccountsUrl is never configured, or in a session with no token (guest/not authenticated).","commonSituations":"Missing accounts service URL in app configuration (config/env not wired); integration invoked at startup before authentication completes; token cleared after logout; running the integration in tests without metadata setup.","solutions":["Configure the accounts service URL so login.metadata.AccountsUrl is defined before initializing the integration.","Ensure the user is logged in and presentation.metadata.Token is set before requesting the integration client.","Defer integration client creation until after authentication completes (login event / auth ready state).","Log which of the two values is undefined to distinguish config vs session problems."],"exampleFix":"// before\nconst accountsUrl = getMetadata(login.metadata.AccountsUrl)\nconst token = getMetadata(presentation.metadata.Token)\nif (accountsUrl === undefined || token === undefined) {\n  throw new Error('Accounts URL or token is not defined')\n}\n// after\nconst accountsUrl = getMetadata(login.metadata.AccountsUrl)\nconst token = getMetadata(presentation.metadata.Token)\nif (accountsUrl === undefined || token === undefined) {\n  throw new Error(`Accounts URL or token is not defined (accountsUrl: ${accountsUrl === undefined ? 'missing' : 'ok'}, token: ${token === undefined ? 'missing' : 'ok'})`)\n}","handlingStrategy":"validation","validationCode":"const accountsUrl = getMetadata(login.metadata.AccountsUrl)\nconst token = getMetadata(presentation.metadata.Token)\nif (accountsUrl === undefined) {\n  throw new Error('Gmail integration requires login.metadata.AccountsUrl to be configured')\n}\nif (token === undefined) {\n  throw new Error('Gmail integration requires an authenticated session token')\n}\nconst client = await integrationClient()","typeGuard":"function hasAuthMetadata (m: { accountsUrl: string | undefined, token: string | undefined }): m is { accountsUrl: string, token: string } {\n  return m.accountsUrl !== undefined && m.token !== undefined\n}","tryCatchPattern":"try {\n  const client = await integrationClient()\n  // use client\n} catch (err) {\n  if (err instanceof Error && err.message === 'Accounts URL or token is not defined') {\n    console.error('Gmail integration misconfigured: check accounts URL config and login state')\n    return\n  }\n  throw err\n}","preventionTips":["Configure the accounts service URL for every deployment/environment.","Create integration clients only after authentication completes.","Centralize metadata checks in one auth helper used by all integrations.","In CI/tests, seed login and presentation metadata before exercising integration code."],"tags":["authentication","configuration","metadata","huly-platform"],"backgroundTag":"missing-env-var","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}