mastra-ai/mastra · error · Error

Not logged in

Error message

Not logged in

What it means

setCurrentOrgId persists the selected organization into stored credentials. It first loads credentials; if none exist (the user never logged in), it throws 'Not logged in' because there is no credential object to update. Called by resolveCurrentOrg and switchOrgAction during org selection.

Source

Thrown at packages/cli/src/commands/auth/credentials.ts:75

    await unlink(CREDENTIALS_FILE);
  } catch {
    // file doesn't exist, that's fine
  }
}

export async function getCurrentOrgId(): Promise<string | null> {
  // CI/CD headless path
  const envOrgId = process.env.MASTRA_ORG_ID;
  if (envOrgId) return envOrgId;

  const creds = await loadCredentials();
  if (!creds) return null;
  return creds.currentOrgId ?? creds.organizationId;
}

export async function setCurrentOrgId(orgId: string): Promise<void> {
  const creds = await loadCredentials();
  if (!creds) throw new Error('Not logged in');
  creds.currentOrgId = orgId;
  await saveCredentials(creds);
}

function isWSL(): boolean {
  if (process.platform !== 'linux') return false;
  try {
    // WSL kernels contain "microsoft" or "WSL" in the version string
    return /microsoft|wsl/i.test(release()) || /microsoft|wsl/i.test(readFileSync('/proc/version', 'utf-8'));
  } catch {
    return false;
  }
}

function openBrowser(url: string) {
  // Use execFileSync (shell: false) to avoid shell-injection via the URL.
  if (process.platform === 'darwin') {
    execFileSync('open', [url]);

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Run `mastra auth login` first, then switch orgs with `mastra auth orgs`.
  2. Set MASTRA_API_TOKEN if operating non-interactively (org selection then handled per command).
  3. Verify the credentials file exists under the expected home directory.

Example fix

// before
mastra auth orgs org_abc123  # -> Error: Not logged in
// after
mastra auth login
mastra auth orgs org_abc123
Defensive patterns

Strategy: validation

Validate before calling

const { loadCredentials } = await import('./credentials.js');
const creds = await loadCredentials();
if (!creds) {
  throw new Error('Not logged in: run `mastra auth login` before switching orgs');
}
await setCurrentOrgId(orgId);

Try / catch

try {
  await setCurrentOrgId(orgId);
} catch (err) {
  if (err instanceof Error && err.message === 'Not logged in') {
    console.error('Run `mastra auth login` first.');
    return;
  }
  throw err;
}

Prevention

When it happens

Trigger: `mastra auth orgs <orgId>` (switchOrgAction) or any command resolving the current org calls setCurrentOrgId while loadCredentials() returns null — no credentials file on disk.

Common situations: Running org-switch on a fresh machine; running before ever executing `mastra auth login`; CI environments without credentials or MASTRA_API_TOKEN; credentials file removed between commands.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/008b5198bd09ea48. Report an issue: GitHub.