{"record":{"id":"ec89ec7bbb9c09ac","repo":"plandex-ai/plandex","slug":"error-refreshing-token-auth-not-loaded","errorCode":null,"errorMessage":"error refreshing token: auth not loaded","messagePattern":"error refreshing token: auth not loaded","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/cli/auth/auth.go","lineNumber":92,"sourceCode":"\t\t\t// still no org--exit now\n\t\t\tterm.OutputErrorAndExit(\"No org\")\n\t\t}\n\n\t\tCurrent.OrgId = org.Id\n\t\tCurrent.OrgName = org.Name\n\t\tCurrent.IntegratedModelsMode = org.IntegratedModelsMode\n\n\t\terr = writeCurrentAuth()\n\n\t\tif err != nil {\n\t\t\tterm.OutputErrorAndExit(\"Error writing auth: %v\", err)\n\t\t}\n\t}\n}\n\nfunc RefreshInvalidToken() error {\n\tif Current == nil {\n\t\treturn fmt.Errorf(\"error refreshing token: auth not loaded\")\n\t}\n\tres, err := verifyEmail(Current.Email, Current.Host)\n\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error verifying email: %v\", err)\n\t}\n\n\tif res.hasAccount {\n\t\treturn signIn(Current.Email, res.pin, Current.Host)\n\t} else {\n\t\thost := Current.Host\n\t\tif host == \"\" {\n\t\t\thost = \"Plandex Cloud\"\n\t\t}\n\n\t\tterm.OutputErrorAndExit(\"Account %s not found on %s\", Current.Email, host)\n\t}\n","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/cli/auth/auth.go#L74-L110","documentation":"RefreshInvalidToken re-verifies the user's email and re-signs-in to obtain a fresh token after an invalid/expired token response. It guards with `if Current == nil` because refreshing requires the stored email/host. Without loaded auth there is nothing to refresh, so it returns this error immediately.","triggerScenarios":"refreshAuthIfNeeded calls RefreshInvalidToken after an API round trip reports an invalid token, but `Current` was never set (auth.json missing, load failed, or refresh path invoked before MustResolveAuth).","commonSituations":"Race or ordering bug where token refresh runs before auth load; auth.json deleted while the CLI is running; testing/embedding that calls refresh APIs directly without sign-in.","solutions":["Ensure auth.MustResolveAuth ran before any request that can trigger refreshAuthIfNeeded","Re-run sign-in to recreate auth.json and load Current","Check disk state: fs.HomeAuthPath should exist and parse as shared.ClientAuth","Fix initialization order in custom code so refresh is never called before auth load"],"exampleFix":"// before\nerr := auth.RefreshInvalidToken() // error refreshing token: auth not loaded\n// after\nif auth.Current == nil {\n\tauth.MustResolveAuth(false)\n}\nerr := auth.RefreshInvalidToken()","handlingStrategy":"validation","validationCode":"if auth.Current == nil {\n\treturn fmt.Errorf(\"cannot refresh token: no auth loaded; run sign-in first\")\n}","typeGuard":"func canRefresh() bool { return auth.Current != nil && auth.Current.Email != \"\" }","tryCatchPattern":"err := auth.RefreshInvalidToken()\nif err != nil {\n\tif strings.Contains(err.Error(), \"auth not loaded\") {\n\t\tauth.MustResolveAuth(false)\n\t\terr = auth.RefreshInvalidToken()\n\t}\n\tif err != nil { term.OutputErrorAndExit(\"%v\", err) }\n}","preventionTips":["Ensure auth load (MustResolveAuth) always precedes refresh logic","Don't delete auth.json while the CLI is running","In tests/embedding, stub or skip refresh when auth is intentionally unset","Keep email/host populated in auth.json"],"tags":["auth","token","refresh"],"backgroundTag":"auth-not-loaded","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}