Spaces:
Running
Running
Commit
Β·
ae27f4a
1
Parent(s):
0d7ac2b
Simplify authentication flow to fix login issues
Browse filesπ§ Simplification:
- Remove complex URL parameter handling
- Remove unnecessary OAuth success detection
- Simplify AuthContext initialization
- Add comprehensive logging for debugging
- Remove redundant event listeners
π― Goal:
Return to a more reliable authentication flow like the
original full-page login system but with popup modal.
The previous complex state management was causing
authentication loops and unreliable state detection.
- backend/routers/auth.py +2 -2
- frontend/src/context/AuthContext.tsx +26 -28
backend/routers/auth.py
CHANGED
|
@@ -249,8 +249,8 @@ async def oauth_callback(request: Request, code: str, state: str):
|
|
| 249 |
|
| 250 |
logger.info(f"User logged in: {user_info.get('name')} ({user_info.get('login')})")
|
| 251 |
|
| 252 |
-
# Redirect to main application
|
| 253 |
-
return RedirectResponse(url="
|
| 254 |
|
| 255 |
|
| 256 |
@router.get("/logout")
|
|
|
|
| 249 |
|
| 250 |
logger.info(f"User logged in: {user_info.get('name')} ({user_info.get('login')})")
|
| 251 |
|
| 252 |
+
# Redirect to main application
|
| 253 |
+
return RedirectResponse(url="/", status_code=302)
|
| 254 |
|
| 255 |
|
| 256 |
@router.get("/logout")
|
frontend/src/context/AuthContext.tsx
CHANGED
|
@@ -57,16 +57,20 @@ export function AuthProvider({ children }: AuthProviderProps) {
|
|
| 57 |
const checkAuthStatus = async () => {
|
| 58 |
try {
|
| 59 |
setIsLoading(true);
|
|
|
|
|
|
|
| 60 |
const response = await fetch("/auth/status");
|
| 61 |
const data = await response.json();
|
| 62 |
|
|
|
|
|
|
|
| 63 |
setIsHFSpaces(data.environment === "huggingface_spaces");
|
| 64 |
setAuthRequired(
|
| 65 |
data.login_required && data.environment === "huggingface_spaces"
|
| 66 |
);
|
| 67 |
|
| 68 |
if (data.user_authenticated && data.user_info) {
|
| 69 |
-
|
| 70 |
id: data.user_info.id || "unknown",
|
| 71 |
username: data.user_info.username || "Unknown User",
|
| 72 |
name:
|
|
@@ -74,26 +78,36 @@ export function AuthProvider({ children }: AuthProviderProps) {
|
|
| 74 |
email: data.user_info.email,
|
| 75 |
avatar_url: data.user_info.avatar_url,
|
| 76 |
auth_method: data.user_info.auth_method || "unknown",
|
| 77 |
-
}
|
|
|
|
|
|
|
| 78 |
setShowLoginModal(false);
|
| 79 |
-
console.log(
|
| 80 |
-
"β
User authenticated successfully:",
|
| 81 |
-
data.user_info.username
|
| 82 |
-
);
|
| 83 |
} else {
|
| 84 |
setUser(null);
|
| 85 |
-
console.log(
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
if (data.environment === "huggingface_spaces" && data.login_required) {
|
|
|
|
| 88 |
setShowLoginModal(true);
|
|
|
|
|
|
|
|
|
|
| 89 |
}
|
| 90 |
}
|
| 91 |
} catch (error) {
|
| 92 |
-
console.error("Failed to check auth status:", error);
|
| 93 |
setUser(null);
|
| 94 |
// Assume local development if fetch fails
|
| 95 |
setIsHFSpaces(false);
|
| 96 |
setAuthRequired(false);
|
|
|
|
| 97 |
} finally {
|
| 98 |
setIsLoading(false);
|
| 99 |
}
|
|
@@ -130,37 +144,21 @@ export function AuthProvider({ children }: AuthProviderProps) {
|
|
| 130 |
};
|
| 131 |
|
| 132 |
useEffect(() => {
|
| 133 |
-
//
|
| 134 |
-
|
| 135 |
-
if (urlParams.get('auth') === 'success') {
|
| 136 |
-
console.log("Authentication success detected - refreshing status");
|
| 137 |
-
// Remove the parameter from URL
|
| 138 |
-
window.history.replaceState({}, document.title, window.location.pathname);
|
| 139 |
-
// Force auth status check
|
| 140 |
-
setTimeout(() => checkAuthStatus(), 100);
|
| 141 |
-
} else {
|
| 142 |
-
checkAuthStatus();
|
| 143 |
-
}
|
| 144 |
|
| 145 |
// Listen for auth-required events from API calls
|
| 146 |
const handleAuthRequired = () => {
|
|
|
|
| 147 |
if (authRequired && !isAuthenticated) {
|
| 148 |
setShowLoginModal(true);
|
| 149 |
}
|
| 150 |
};
|
| 151 |
|
| 152 |
-
// Listen for page focus to refresh auth status (useful after OAuth redirect)
|
| 153 |
-
const handleFocus = () => {
|
| 154 |
-
console.log("Page focused - checking auth status");
|
| 155 |
-
checkAuthStatus();
|
| 156 |
-
};
|
| 157 |
-
|
| 158 |
window.addEventListener("auth-required", handleAuthRequired);
|
| 159 |
-
window.addEventListener("focus", handleFocus);
|
| 160 |
|
| 161 |
return () => {
|
| 162 |
window.removeEventListener("auth-required", handleAuthRequired);
|
| 163 |
-
window.removeEventListener("focus", handleFocus);
|
| 164 |
};
|
| 165 |
}, []);
|
| 166 |
|
|
|
|
| 57 |
const checkAuthStatus = async () => {
|
| 58 |
try {
|
| 59 |
setIsLoading(true);
|
| 60 |
+
console.log("π Checking authentication status...");
|
| 61 |
+
|
| 62 |
const response = await fetch("/auth/status");
|
| 63 |
const data = await response.json();
|
| 64 |
|
| 65 |
+
console.log("π Auth status response:", data);
|
| 66 |
+
|
| 67 |
setIsHFSpaces(data.environment === "huggingface_spaces");
|
| 68 |
setAuthRequired(
|
| 69 |
data.login_required && data.environment === "huggingface_spaces"
|
| 70 |
);
|
| 71 |
|
| 72 |
if (data.user_authenticated && data.user_info) {
|
| 73 |
+
const userData = {
|
| 74 |
id: data.user_info.id || "unknown",
|
| 75 |
username: data.user_info.username || "Unknown User",
|
| 76 |
name:
|
|
|
|
| 78 |
email: data.user_info.email,
|
| 79 |
avatar_url: data.user_info.avatar_url,
|
| 80 |
auth_method: data.user_info.auth_method || "unknown",
|
| 81 |
+
};
|
| 82 |
+
|
| 83 |
+
setUser(userData);
|
| 84 |
setShowLoginModal(false);
|
| 85 |
+
console.log("β
User authenticated successfully:", userData.username);
|
|
|
|
|
|
|
|
|
|
| 86 |
} else {
|
| 87 |
setUser(null);
|
| 88 |
+
console.log(
|
| 89 |
+
"β User not authenticated, environment:",
|
| 90 |
+
data.environment,
|
| 91 |
+
"auth required:",
|
| 92 |
+
data.login_required
|
| 93 |
+
);
|
| 94 |
+
|
| 95 |
+
// Only show login modal in HF Spaces when auth is required
|
| 96 |
if (data.environment === "huggingface_spaces" && data.login_required) {
|
| 97 |
+
console.log("π Showing login modal for HF Spaces");
|
| 98 |
setShowLoginModal(true);
|
| 99 |
+
} else {
|
| 100 |
+
console.log("π Local development - no login required");
|
| 101 |
+
setShowLoginModal(false);
|
| 102 |
}
|
| 103 |
}
|
| 104 |
} catch (error) {
|
| 105 |
+
console.error("β Failed to check auth status:", error);
|
| 106 |
setUser(null);
|
| 107 |
// Assume local development if fetch fails
|
| 108 |
setIsHFSpaces(false);
|
| 109 |
setAuthRequired(false);
|
| 110 |
+
setShowLoginModal(false);
|
| 111 |
} finally {
|
| 112 |
setIsLoading(false);
|
| 113 |
}
|
|
|
|
| 144 |
};
|
| 145 |
|
| 146 |
useEffect(() => {
|
| 147 |
+
// Initial auth check
|
| 148 |
+
checkAuthStatus();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
// Listen for auth-required events from API calls
|
| 151 |
const handleAuthRequired = () => {
|
| 152 |
+
console.log("π Auth required event received");
|
| 153 |
if (authRequired && !isAuthenticated) {
|
| 154 |
setShowLoginModal(true);
|
| 155 |
}
|
| 156 |
};
|
| 157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
window.addEventListener("auth-required", handleAuthRequired);
|
|
|
|
| 159 |
|
| 160 |
return () => {
|
| 161 |
window.removeEventListener("auth-required", handleAuthRequired);
|
|
|
|
| 162 |
};
|
| 163 |
}, []);
|
| 164 |
|