Skip to content

Instantly share code, notes, and snippets.

@troykelly
Created March 14, 2026 07:07
Show Gist options
  • Select an option

  • Save troykelly/13701bc9dcf1943a41df40cace64fe49 to your computer and use it in GitHub Desktop.

Select an option

Save troykelly/13701bc9dcf1943a41df40cace64fe49 to your computer and use it in GitHub Desktop.
better-ccflare oAuth Patch
diff --git a/packages/http-api/src/handlers/accounts.ts b/packages/http-api/src/handlers/accounts.ts
index d954345..8ef3284 100644
--- a/packages/http-api/src/handlers/accounts.ts
+++ b/packages/http-api/src/handlers/accounts.ts
@@ -163,22 +163,17 @@ export function createAccountsListHandler(dbOps: DatabaseOperations) {
// Use unified rate limit status if available
if (account.rate_limit_status) {
rateLimitStatus = account.rate_limit_status;
- if (account.rate_limit_reset && account.rate_limit_reset > now) {
- const minutesLeft = Math.ceil(
- (account.rate_limit_reset - now) / 60000,
- );
+ const resetMs = Number(account.rate_limit_reset);
+ if (resetMs && resetMs > now) {
+ const minutesLeft = Math.ceil((resetMs - now) / 60000);
rateLimitStatus = `${account.rate_limit_status} (${minutesLeft}m)`;
}
- } else if (
- account.rate_limited &&
- account.rate_limited_until &&
- account.rate_limited_until > now
- ) {
- // Fall back to legacy rate limit check
- const minutesLeft = Math.ceil(
- (account.rate_limited_until - now) / 60000,
- );
- rateLimitStatus = `Rate limited (${minutesLeft}m)`;
+ } else if (account.rate_limited && account.rate_limited_until) {
+ const limitedMs = Number(account.rate_limited_until);
+ if (limitedMs > now) {
+ const minutesLeft = Math.ceil((limitedMs - now) / 60000);
+ rateLimitStatus = `Rate limited (${minutesLeft}m)`;
+ }
}
// Get usage data from cache for Anthropic and NanoGPT accounts
@@ -328,24 +323,24 @@ export function createAccountsListHandler(dbOps: DatabaseOperations) {
id: account.id,
name: account.name,
provider: account.provider || "anthropic",
- requestCount: account.request_count,
- totalRequests: account.total_requests,
+ requestCount: Number(account.request_count) || 0,
+ totalRequests: Number(account.total_requests) || 0,
lastUsed: account.last_used
- ? new Date(account.last_used).toISOString()
+ ? new Date(Number(account.last_used)).toISOString()
: null,
- created: new Date(account.created_at).toISOString(),
+ created: new Date(Number(account.created_at)).toISOString(),
paused: account.paused === 1,
- priority: account.priority,
+ priority: Number(account.priority) || 0,
tokenStatus: account.token_valid ? "valid" : "expired",
tokenExpiresAt: account.expires_at
- ? new Date(account.expires_at).toISOString()
+ ? new Date(Number(account.expires_at)).toISOString()
: null,
rateLimitStatus,
rateLimitReset: account.rate_limit_reset
- ? new Date(account.rate_limit_reset).toISOString()
+ ? new Date(Number(account.rate_limit_reset)).toISOString()
: null,
- rateLimitRemaining: account.rate_limit_remaining,
- rateLimitedUntil: account.rate_limited_until || null,
+ rateLimitRemaining: account.rate_limit_remaining != null ? Number(account.rate_limit_remaining) : null,
+ rateLimitedUntil: account.rate_limited_until ? Number(account.rate_limited_until) : null,
sessionInfo: account.session_info || "",
autoFallbackEnabled: account.auto_fallback_enabled === 1,
autoRefreshEnabled: account.auto_refresh_enabled === 1,
diff --git a/packages/providers/src/providers/anthropic/oauth.ts b/packages/providers/src/providers/anthropic/oauth.ts
index 8365839..4dbfaf8 100644
--- a/packages/providers/src/providers/anthropic/oauth.ts
+++ b/packages/providers/src/providers/anthropic/oauth.ts
@@ -35,7 +35,7 @@ export class AnthropicOAuthProvider implements OAuthProvider {
tokenUrl: "https://console.anthropic.com/v1/oauth/token",
clientId: "", // Will be passed from config
scopes: ["org:create_api_key", "user:profile", "user:inference"],
- redirectUri: "https://console.anthropic.com/oauth/code/callback",
+ redirectUri: "https://platform.claude.com/oauth/code/callback",
mode,
};
}
@@ -44,26 +44,18 @@ export class AnthropicOAuthProvider implements OAuthProvider {
// Generate secure random state for CSRF protection (separate from PKCE verifier)
const state = this.generateSecureRandomState();
- // For claude-oauth mode (Claude CLI), use the login flow that redirects to OAuth
+ // For claude-oauth mode, go directly to the OAuth authorize endpoint
if (config.mode === "claude-oauth") {
- const baseUrl = config.authorizeUrl.split("/oauth/authorize")[0];
- const oauthParams = new URLSearchParams();
- oauthParams.set("code", "true");
- oauthParams.set("client_id", config.clientId);
- oauthParams.set("response_type", "code");
- oauthParams.set("redirect_uri", config.redirectUri);
- oauthParams.set("scope", config.scopes.join(" "));
- oauthParams.set("code_challenge", pkce.challenge);
- oauthParams.set("code_challenge_method", "S256");
- oauthParams.set("state", state);
-
- const returnTo = `/oauth/authorize?${oauthParams.toString()}`;
-
- const loginUrl = new URL(`${baseUrl}/login`);
- loginUrl.searchParams.set("selectAccount", "true");
- loginUrl.searchParams.set("returnTo", returnTo);
-
- return loginUrl.toString();
+ const url = new URL(config.authorizeUrl);
+ url.searchParams.set("code", "true");
+ url.searchParams.set("client_id", config.clientId);
+ url.searchParams.set("response_type", "code");
+ url.searchParams.set("redirect_uri", config.redirectUri);
+ url.searchParams.set("scope", config.scopes.join(" "));
+ url.searchParams.set("code_challenge", pkce.challenge);
+ url.searchParams.set("code_challenge_method", "S256");
+ url.searchParams.set("state", state);
+ return url.toString();
} else {
// For console mode, use direct OAuth flow
const url = new URL(config.authorizeUrl);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment