update
Browse filesFiles changed (2)
- backend_api.py+21-1
- frontend/src/lib/api.ts+19-11
293 unmodified lines294295296297298299300293 unmodified linesusername = parts[2] if len(parts) > 2 else "user"return MockAuth(token, username)# Regular token (OAuth access token passed directly)return MockAuth(token, None)293 unmodified lines294295296297298299300301302303304305306307308309310311312313314315316317318319320293 unmodified linesusername = parts[2] if len(parts) > 2 else "user"return MockAuth(token, username)# Regular OAuth access token passed directly - try to fetch username from HF# This happens when frontend sends OAuth token after OAuth callbackif token and len(token) > 20:try:from huggingface_hub import HfApihf_api = HfApi(token=token)user_info = hf_api.whoami()username = (user_info.get("preferred_username") oruser_info.get("name") oruser_info.get("sub") or"user")print(f"[Auth] Fetched username from OAuth token: {username}")return MockAuth(token, username)except Exception as e:print(f"[Auth] Could not fetch username from OAuth token: {e}")# Return with token but no username - deployment will try to fetch itreturn MockAuth(token, None)# Fallback: token with no usernamereturn MockAuth(token, None)
123456747 unmodified lines5556575859606162636465664 unmodified lines71727374757677787980199 unmodified lines280281282283284285286287// API client for AnyCoder backendimport axios, { AxiosInstance } from 'axios';import { getStoredSessionToken } from './auth'; // NEW: Import session tokenimport type {Model,AuthStatus,47 unmodified lines// Add auth token to requests if availablethis.client.interceptors.request.use((config) => {// Use session token instead of OAuth token for session trackingconst sessionToken = getStoredSessionToken();if (sessionToken) {config.headers.Authorization = `Bearer ${sessionToken}`;} else if (this.token) {// Fallback to OAuth token if no session tokenconfig.headers.Authorization = `Bearer ${this.token}`;}return config;4 unmodified lines(response) => response,(error) => {// Handle 401 errors (expired/invalid authentication)if (error.response && error.response.status === 401) {// Clear authentication dataif (typeof window !== 'undefined') {localStorage.removeItem('hf_oauth_token');localStorage.removeItem('hf_user_info');this.token = null;199 unmodified linesmethod: 'POST',headers: {'Content-Type': 'application/json',...(getStoredSessionToken() ? { 'Authorization': `Bearer ${getStoredSessionToken()}` } :this.token ? { 'Authorization': `Bearer ${this.token}` } : {}),},body: JSON.stringify(request),signal: abortController.signal,12345647 unmodified lines54555657585960614 unmodified lines666768697071727374757677787980818283848586878889199 unmodified lines289290291292293294295// API client for AnyCoder backendimport axios, { AxiosInstance } from 'axios';import type {Model,AuthStatus,47 unmodified lines// Add auth token to requests if availablethis.client.interceptors.request.use((config) => {// ALWAYS use OAuth token primarily, session token is for backend tracking onlyif (this.token) {config.headers.Authorization = `Bearer ${this.token}`;}return config;4 unmodified lines(response) => response,(error) => {// Handle 401 errors (expired/invalid authentication)// ONLY log out on specific auth errors, not all 401sif (error.response && error.response.status === 401) {const errorData = error.response.data;const errorMessage = errorData?.detail || errorData?.message || '';// Only log out if it's an authentication/session issue// Don't log out for permission errors on specific resourcesconst shouldLogout =errorMessage.includes('Authentication required') ||errorMessage.includes('Invalid token') ||errorMessage.includes('Token expired') ||errorMessage.includes('Session expired') ||error.config?.url?.includes('/auth/');if (shouldLogout && typeof window !== 'undefined') {// Clear ALL authentication data including session tokenlocalStorage.removeItem('hf_oauth_token');localStorage.removeItem('hf_session_token');localStorage.removeItem('hf_user_info');this.token = null;199 unmodified linesmethod: 'POST',headers: {'Content-Type': 'application/json',...(this.token ? { 'Authorization': `Bearer ${this.token}` } : {}),},body: JSON.stringify(request),signal: abortController.signal,
backend_api.py+21-1
frontend/src/lib/api.ts+19-11