{"record":{"id":"d618127967ae0e81","repo":"rocksdanister/lively","slug":"token-not-found","errorCode":null,"errorMessage":"Token not found.","messagePattern":"Token not found\\.","errorType":"exception","errorClass":"UnauthorizedAccessException","httpStatus":401,"severity":"error","filePath":"src/Lively/Lively.Gallery.Client/GalleryClient.cs","lineNumber":296,"sourceCode":"            var httpResp = await InternalSendAsync(message, isRetry, requireAuth, HttpCompletionOption.ResponseContentRead);\n            if (httpResp.StatusCode == HttpStatusCode.Unauthorized)\n                throw new UnauthorizedAccessException(ApiErrors.TokensExpired);\n            var content = await httpResp.Content.ReadAsStringAsync();\n            var response = JsonConvert.DeserializeObject<ApiResponse<T>>(content);\n            if (response == null)\n                response = new();\n            if (response.Errors != null)\n            {\n                throw new ApiException(response.Errors);\n            }\n            response.StatusCode = (int)httpResp.StatusCode;\n            return response;\n        }\n\n        private async Task<HttpResponseMessage> InternalSendAsync(HttpRequestMessage message, bool isRetry, bool requireAuth = true, HttpCompletionOption option = HttpCompletionOption.ResponseContentRead)\n        {\n            if (requireAuth && Tokens?.AccessToken == null)\n                throw new UnauthorizedAccessException(\"Token not found.\");\n\n            if (requireAuth && Tokens?.AccessToken != null)\n                message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(\"Bearer\", Tokens.AccessToken);\n            //The request message was already sent. Cannot send the same request message multiple times.\n            var clone = message.Clone();\n            var httpResp = await _client.SendAsync(message, option);\n            var requireRefreshingTokens = !isRetry && httpResp.StatusCode == HttpStatusCode.Unauthorized;\n            if (requireRefreshingTokens)\n            {\n                var result = await RefreshTokensAsync();\n\n                var tokens = result;\n                _tokenStore.Set(tokens.AccessToken, tokens.RefreshToken, _tokenStore.Get().Provider, tokens.Expiration);\n                return await InternalSendAsync(clone, true, requireAuth, option);\n\n            }\n            clone.Dispose();\n            return httpResp;","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/rocksdanister/lively/blob/c1036feb664960722e34bf4309042c247d6a909d/src/Lively/Lively.Gallery.Client/GalleryClient.cs#L278-L314","documentation":"Thrown by InternalSendAsync when a request requires auth (requireAuth=true, the default) but the token store has no access token at all. It is the pre-flight gate: do not even hit the network without credentials.","triggerScenarios":"Any SendAsync/DownloadFile call with requireAuth=true while Tokens?.AccessToken is null — i.e. the user is not logged in or the token store is empty.","commonSituations":"Calling a gallery method before the user signs in; after a logout but the UI still holds stale view-models issuing requests; token store failed to load on startup.","solutions":["Guard gallery calls with a login-state check; if not authenticated, route to login instead of calling.","Ensure token store persistence loads correctly on startup before any gallery UI binds.","Catch UnauthorizedAccessException and treat it as 'needs login' uniformly with errors 5/6/8."],"exampleFix":"// before\nif (requireAuth && Tokens?.AccessToken == null)\n    throw new UnauthorizedAccessException(\"Token not found.\");\n\n// after\nif (requireAuth && Tokens?.AccessToken == null)\n    throw new UnauthorizedAccessException(\"NOT_LOGGED_IN\");","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(client.Tokens?.AccessToken))\n{\n    await NavigateToLogin();\n    return;\n}","typeGuard":"static bool IsLoggedIn(GalleryClient c) => !string.IsNullOrEmpty(c.Tokens?.AccessToken);","tryCatchPattern":"try { await client.SomeAuthedCall(); }\ncatch (UnauthorizedAccessException ex) when (ex.Message.Contains(\"Token not found\"))\n{ await NavigateToLogin(); }","preventionTips":["Gate all gallery UI behind login state.","Ensure the token store loads on startup before any gallery view activates.","Treat 'Token not found' uniformly with other UnauthorizedAccessExceptions."],"tags":["auth","tokens","gallery","validation"],"backgroundTag":null,"analyzedSha":"c1036feb664960722e34bf4309042c247d6a909d","analyzedAt":"2026-08-13T13:03:12.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}