Spaces:
Sleeping
Sleeping
Commit
·
1c4b979
1
Parent(s):
eb7193f
Fix: Auto-trigger auth modal in HF Spaces
Browse files🐛 Problem Fixed:
- Users could access the app without authentication in HF Spaces
- Auth modal was not automatically shown to new users
- App was working in 'anonymous' mode instead of requiring login
🔧 Solution:
- Added automatic auth modal trigger in HF Spaces environment
- Check if user is authenticated on app startup
- Show auth modal immediately if not authenticated in HF Spaces
- Maintain no-auth behavior for local development
💡 How it works:
1. App loads in HF Spaces
2. AuthContext checks for existing authentication
3. If no auth found AND in HF Spaces → show auth modal
4. If in local dev → continue without auth
Now users will see the auth modal immediately when visiting
the HF Spaces deployment, ensuring proper authentication flow.
frontend/src/context/AuthContext.tsx
CHANGED
|
@@ -90,9 +90,10 @@ function convertHFOAuthResult(hfResult: HFOAuthResult): OAuthResult {
|
|
| 90 |
const userInfo = hfResult.userInfo as any; // Type assertion for flexibility
|
| 91 |
return {
|
| 92 |
accessToken: hfResult.accessToken,
|
| 93 |
-
accessTokenExpiresAt:
|
| 94 |
-
|
| 95 |
-
|
|
|
|
| 96 |
userInfo: {
|
| 97 |
id: userInfo.sub || userInfo.id || "unknown",
|
| 98 |
name: userInfo.name || userInfo.preferred_username || "Unknown User",
|
|
@@ -147,7 +148,16 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
|
| 147 |
localStorage.setItem(STORAGE_KEY, JSON.stringify(normalizedResult));
|
| 148 |
dispatch({ type: "AUTH_SUCCESS", payload: normalizedResult });
|
| 149 |
} else {
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
}
|
| 152 |
} catch (error) {
|
| 153 |
console.error("Auth check failed:", error);
|
|
|
|
| 90 |
const userInfo = hfResult.userInfo as any; // Type assertion for flexibility
|
| 91 |
return {
|
| 92 |
accessToken: hfResult.accessToken,
|
| 93 |
+
accessTokenExpiresAt:
|
| 94 |
+
hfResult.accessTokenExpiresAt instanceof Date
|
| 95 |
+
? hfResult.accessTokenExpiresAt.toISOString()
|
| 96 |
+
: hfResult.accessTokenExpiresAt,
|
| 97 |
userInfo: {
|
| 98 |
id: userInfo.sub || userInfo.id || "unknown",
|
| 99 |
name: userInfo.name || userInfo.preferred_username || "Unknown User",
|
|
|
|
| 148 |
localStorage.setItem(STORAGE_KEY, JSON.stringify(normalizedResult));
|
| 149 |
dispatch({ type: "AUTH_SUCCESS", payload: normalizedResult });
|
| 150 |
} else {
|
| 151 |
+
// No OAuth redirect found, check if we need to show auth modal
|
| 152 |
+
const isHFSpaces = window.huggingface && window.huggingface.variables;
|
| 153 |
+
if (isHFSpaces) {
|
| 154 |
+
// In HF Spaces, we require authentication - show the modal
|
| 155 |
+
dispatch({ type: "SET_LOADING", payload: false });
|
| 156 |
+
openModal("auth-login", "Sign in to AgentGraph");
|
| 157 |
+
} else {
|
| 158 |
+
// In local development, no auth required
|
| 159 |
+
dispatch({ type: "SET_LOADING", payload: false });
|
| 160 |
+
}
|
| 161 |
}
|
| 162 |
} catch (error) {
|
| 163 |
console.error("Auth check failed:", error);
|