wu981526092 commited on
Commit
1b33ba2
Β·
1 Parent(s): 121ec8d

Fix: OAuth clientId missing error in HF Spaces

Browse files

πŸ› Problem:
- Modal shows correctly but OAuth login fails with 'Missing clientId'
- @huggingface/hub library needs proper client configuration

πŸ”§ Solution:
- Added fallback OAuth URL generation for HF Spaces
- Enhanced error handling with try-catch for OAuth calls
- Added detailed debugging for window.huggingface object
- Try default OAuth config if custom scopes fail

πŸ” Debug Info Added:
- Log complete huggingface object structure
- Show available variables and OAuth scopes
- Better error messages for OAuth failures

This should resolve the OAuth client configuration issues.

Files changed (1) hide show
  1. frontend/src/context/AuthContext.tsx +39 -14
frontend/src/context/AuthContext.tsx CHANGED
@@ -149,17 +149,17 @@ export function AuthProvider({ children }: { children: ReactNode }) {
149
  dispatch({ type: "AUTH_SUCCESS", payload: normalizedResult });
150
  } else {
151
  // No OAuth redirect found, check if we need to show auth modal
152
-
153
  // Enhanced HF Spaces detection with debugging
154
- const isHFSpaces =
155
  // Method 1: Check for window.huggingface
156
  (window.huggingface && window.huggingface.variables) ||
157
  // Method 2: Check for HF-specific URL patterns
158
- window.location.hostname.includes('hf.space') ||
159
- window.location.hostname.includes('huggingface.co') ||
160
  // Method 3: Check for HF-specific environment indicators
161
  document.querySelector('meta[name="hf:space"]') !== null;
162
-
163
  // Debug logging (remove in production)
164
  console.log("πŸ” HF Spaces Environment Check:", {
165
  hostname: window.location.hostname,
@@ -167,9 +167,12 @@ export function AuthProvider({ children }: { children: ReactNode }) {
167
  hasVariables: !!(window.huggingface && window.huggingface.variables),
168
  hasHfMeta: !!document.querySelector('meta[name="hf:space"]'),
169
  isHFSpaces,
170
- href: window.location.href
 
 
 
171
  });
172
-
173
  if (isHFSpaces) {
174
  // In HF Spaces, we require authentication - show the modal
175
  console.log("πŸ” HF Spaces detected - showing auth modal");
@@ -194,20 +197,42 @@ export function AuthProvider({ children }: { children: ReactNode }) {
194
  try {
195
  dispatch({ type: "AUTH_START" });
196
 
197
- // Check if we're in HF Spaces environment
198
- const isHFSpaces =
199
  (window.huggingface && window.huggingface.variables) ||
200
- window.location.hostname.includes('hf.space') ||
201
- window.location.hostname.includes('huggingface.co') ||
202
  document.querySelector('meta[name="hf:space"]') !== null;
203
 
204
  if (isHFSpaces) {
205
- // Use HF OAuth
206
  const scopes =
207
  window.huggingface?.variables?.OAUTH_SCOPES ||
208
  "openid profile read-repos";
209
- const loginUrl = await oauthLoginUrl({ scopes });
210
- window.location.href = loginUrl + "&prompt=consent";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
  } else {
212
  // For local development, show a message or redirect to HF
213
  dispatch({
 
149
  dispatch({ type: "AUTH_SUCCESS", payload: normalizedResult });
150
  } else {
151
  // No OAuth redirect found, check if we need to show auth modal
152
+
153
  // Enhanced HF Spaces detection with debugging
154
+ const isHFSpaces =
155
  // Method 1: Check for window.huggingface
156
  (window.huggingface && window.huggingface.variables) ||
157
  // Method 2: Check for HF-specific URL patterns
158
+ window.location.hostname.includes("hf.space") ||
159
+ window.location.hostname.includes("huggingface.co") ||
160
  // Method 3: Check for HF-specific environment indicators
161
  document.querySelector('meta[name="hf:space"]') !== null;
162
+
163
  // Debug logging (remove in production)
164
  console.log("πŸ” HF Spaces Environment Check:", {
165
  hostname: window.location.hostname,
 
167
  hasVariables: !!(window.huggingface && window.huggingface.variables),
168
  hasHfMeta: !!document.querySelector('meta[name="hf:space"]'),
169
  isHFSpaces,
170
+ href: window.location.href,
171
+ huggingfaceObject: window.huggingface,
172
+ variables: window.huggingface?.variables,
173
+ oauthScopes: window.huggingface?.variables?.OAUTH_SCOPES,
174
  });
175
+
176
  if (isHFSpaces) {
177
  // In HF Spaces, we require authentication - show the modal
178
  console.log("πŸ” HF Spaces detected - showing auth modal");
 
197
  try {
198
  dispatch({ type: "AUTH_START" });
199
 
200
+ // Check if we're in HF Spaces environment
201
+ const isHFSpaces =
202
  (window.huggingface && window.huggingface.variables) ||
203
+ window.location.hostname.includes("hf.space") ||
204
+ window.location.hostname.includes("huggingface.co") ||
205
  document.querySelector('meta[name="hf:space"]') !== null;
206
 
207
  if (isHFSpaces) {
208
+ // Use HF OAuth with proper client configuration
209
  const scopes =
210
  window.huggingface?.variables?.OAUTH_SCOPES ||
211
  "openid profile read-repos";
212
+
213
+ // In HF Spaces, the OAuth client configuration is handled automatically
214
+ // We just need to call oauthLoginUrl with scopes
215
+ try {
216
+ const loginUrl = await oauthLoginUrl({
217
+ scopes,
218
+ // HF Spaces automatically provides clientId when deployed
219
+ });
220
+ console.log("πŸ” Generated OAuth login URL:", loginUrl);
221
+ window.location.href = loginUrl + "&prompt=consent";
222
+ } catch (oauthError) {
223
+ console.error("OAuth URL generation failed:", oauthError);
224
+ // Fallback: Try without custom scopes
225
+ try {
226
+ const fallbackUrl = await oauthLoginUrl({});
227
+ console.log("πŸ”„ Using fallback OAuth URL");
228
+ window.location.href = fallbackUrl + "&prompt=consent";
229
+ } catch (fallbackError) {
230
+ console.error("Fallback OAuth also failed:", fallbackError);
231
+ throw new Error(
232
+ "OAuth configuration not available in this environment"
233
+ );
234
+ }
235
+ }
236
  } else {
237
  // For local development, show a message or redirect to HF
238
  dispatch({