Budibase/budibase · error · HTTPError

Unable to resolve current user

Error message

Unable to resolve current user

What it means

getCurrentGlobalUserId derives the caller's global user id from the authenticated context, trying ctx.user.globalId, then converting the metadata _id via getGlobalIDFromUserMetadataID, then falling back to _id. If none yields an id, the request context is not carrying a usable authenticated user, so a 400 is thrown.

Source

Thrown at packages/server/src/api/controllers/ai/chatIdentityLinks.ts:135

const assertSessionMatchesInstance = ({
  workspaceId,
  instance,
}: {
  workspaceId?: string
  instance: string
}) => {
  if (!workspaceId || workspaceId !== instance) {
    throw new HTTPError("Link token is not valid for this workspace", 400)
  }
}

const getCurrentGlobalUserId = (ctx: UserCtx) => {
  const currentUserId =
    ctx.user?.globalId ||
    getGlobalIDFromUserMetadataID(ctx.user?._id || "") ||
    ctx.user?._id
  if (!currentUserId) {
    throw new HTTPError("Unable to resolve current user", 400)
  }
  return currentUserId
}

const providerDisplayName = (provider: ChatIdentityLinkProvider) => {
  if (provider === AgentChannelProvider.MSTEAMS) {
    return "Teams"
  }
  if (provider === AgentChannelProvider.SLACK) {
    return "Slack"
  }

  throw provider satisfies never
}

const renderLinkConfirmationPage = (
  session: ChatIdentityLinkSession,
  action: string

View on GitHub (pinned to a81a902e9a)

Solutions

  1. Ensure the route is authenticated and the auth middleware populates ctx.user before the handler runs
  2. Log in again or supply valid credentials (session cookie / API token) so ctx.user contains an id
  3. Check the user document for missing _id/globalId and repair via user sync
  4. If calling internally, pass a properly shaped ContextUser with at least _id

Example fix

// before
await confirmLink(ctx) // ctx.user undefined -> 400
// after
// ensure auth middleware ran
router.post("/confirm", authMiddleware, confirmLink) // ctx.user populated
Defensive patterns

Strategy: validation

Validate before calling

const userId = ctx.user?.globalId || ctx.user?._id
if (!userId) throw new Error("no authenticated user in context")

Type guard

function hasResolvedUser(user?: ContextUser): user is ContextUser & { _id: string } {
  return !!user && typeof user._id === "string" && user._id.length > 0
}

Try / catch

try {
  await api.confirmLink(instance, token)
} catch (e) {
  if (e instanceof HTTPError && e.status === 400 && e.message === "Unable to resolve current user") {
    // redirect to login / re-run auth middleware
  }
  throw e
}

Prevention

When it happens

Trigger: Calling an endpoint that invokes currentGlobalUserId (e.g. identity link handoff/confirm) with a ctx.user that is empty or lacks both globalId and _id — typically an unauthenticated or improperly populated auth context reaching an authenticated route.

Common situations: Auth middleware bypassed or misordered so ctx.user is never hydrated; internal/service calls invoking controller helpers without a real user context; user metadata documents missing ids after a sync/import issue; testing endpoints without a valid session/cookie or API key.

Related errors


AI-assisted analysis of Budibase/budibase@a81a902e9a (2026-08-29). Data as JSON: /api/errors/c1c19b60b18986c2. Report an issue: GitHub.