Spaces:
Runtime error
Runtime error
Mark-Lasfar commited on
Commit ·
10ac3d0
1
Parent(s): 1b97733
update main.py
Browse files- api/auth.py +14 -5
- main.py +8 -1
- templates/login.html +21 -2
api/auth.py
CHANGED
|
@@ -17,7 +17,7 @@ import os
|
|
| 17 |
import logging
|
| 18 |
import secrets
|
| 19 |
|
| 20 |
-
from api.database import User, OAuthAccount, CustomSQLAlchemyUserDatabase, get_user_db
|
| 21 |
from api.models import UserRead, UserCreate, UserUpdate
|
| 22 |
|
| 23 |
# إعداد اللوقينج
|
|
@@ -110,6 +110,10 @@ class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
|
|
| 110 |
if user:
|
| 111 |
logger.info(f"User found: {user.email}, proceeding with on_after_login")
|
| 112 |
await self.on_after_login(user, request)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
return user
|
| 114 |
else:
|
| 115 |
logger.error(f"No user found for OAuth account with user_id: {existing_oauth_account.user_id}")
|
|
@@ -123,6 +127,10 @@ class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
|
|
| 123 |
await self.add_oauth_account(oauth_account)
|
| 124 |
logger.info(f"User associated: {user.email}, proceeding with on_after_login")
|
| 125 |
await self.on_after_login(user, request)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
return user
|
| 127 |
|
| 128 |
logger.info(f"Creating new user for email: {account_email}")
|
|
@@ -138,13 +146,15 @@ class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
|
|
| 138 |
await self.add_oauth_account(oauth_account)
|
| 139 |
logger.info(f"New user created: {user.email}, proceeding with on_after_login")
|
| 140 |
await self.on_after_login(user, request)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
return user
|
| 142 |
|
| 143 |
-
# استدعاء user manager من get_user_db
|
| 144 |
async def get_user_manager(user_db: CustomSQLAlchemyUserDatabase = Depends(get_user_db)):
|
| 145 |
yield UserManager(user_db)
|
| 146 |
|
| 147 |
-
# OAuth Routers مع معالجة مخصصة لـ GitHub
|
| 148 |
google_oauth_router = get_oauth_router(
|
| 149 |
google_oauth_client,
|
| 150 |
auth_backend,
|
|
@@ -172,7 +182,6 @@ fastapi_users = FastAPIUsers[User, int](
|
|
| 172 |
|
| 173 |
current_active_user = fastapi_users.current_user(active=True, optional=True)
|
| 174 |
|
| 175 |
-
# تضمين الراوترات داخل التطبيق
|
| 176 |
def get_auth_router(app: FastAPI):
|
| 177 |
app.include_router(google_oauth_router, prefix="/auth/google", tags=["auth"])
|
| 178 |
app.include_router(github_oauth_router, prefix="/auth/github", tags=["auth"])
|
|
@@ -180,4 +189,4 @@ def get_auth_router(app: FastAPI):
|
|
| 180 |
app.include_router(fastapi_users.get_register_router(UserRead, UserCreate), prefix="/auth", tags=["auth"])
|
| 181 |
app.include_router(fastapi_users.get_reset_password_router(), prefix="/auth", tags=["auth"])
|
| 182 |
app.include_router(fastapi_users.get_verify_router(UserRead), prefix="/auth", tags=["auth"])
|
| 183 |
-
app.include_router(fastapi_users.get_users_router(UserRead, UserUpdate), prefix="/users", tags=["users"])
|
|
|
|
| 17 |
import logging
|
| 18 |
import secrets
|
| 19 |
|
| 20 |
+
from api.database import User, OAuthAccount, CustomSQLAlchemyUserDatabase, get_user_db
|
| 21 |
from api.models import UserRead, UserCreate, UserUpdate
|
| 22 |
|
| 23 |
# إعداد اللوقينج
|
|
|
|
| 110 |
if user:
|
| 111 |
logger.info(f"User found: {user.email}, proceeding with on_after_login")
|
| 112 |
await self.on_after_login(user, request)
|
| 113 |
+
if request:
|
| 114 |
+
request.session["user_id"] = str(user.id)
|
| 115 |
+
response = RedirectResponse(url="/chat", status_code=302)
|
| 116 |
+
return response
|
| 117 |
return user
|
| 118 |
else:
|
| 119 |
logger.error(f"No user found for OAuth account with user_id: {existing_oauth_account.user_id}")
|
|
|
|
| 127 |
await self.add_oauth_account(oauth_account)
|
| 128 |
logger.info(f"User associated: {user.email}, proceeding with on_after_login")
|
| 129 |
await self.on_after_login(user, request)
|
| 130 |
+
if request:
|
| 131 |
+
request.session["user_id"] = str(user.id)
|
| 132 |
+
response = RedirectResponse(url="/chat", status_code=302)
|
| 133 |
+
return response
|
| 134 |
return user
|
| 135 |
|
| 136 |
logger.info(f"Creating new user for email: {account_email}")
|
|
|
|
| 146 |
await self.add_oauth_account(oauth_account)
|
| 147 |
logger.info(f"New user created: {user.email}, proceeding with on_after_login")
|
| 148 |
await self.on_after_login(user, request)
|
| 149 |
+
if request:
|
| 150 |
+
request.session["user_id"] = str(user.id)
|
| 151 |
+
response = RedirectResponse(url="/chat", status_code=302)
|
| 152 |
+
return response
|
| 153 |
return user
|
| 154 |
|
|
|
|
| 155 |
async def get_user_manager(user_db: CustomSQLAlchemyUserDatabase = Depends(get_user_db)):
|
| 156 |
yield UserManager(user_db)
|
| 157 |
|
|
|
|
| 158 |
google_oauth_router = get_oauth_router(
|
| 159 |
google_oauth_client,
|
| 160 |
auth_backend,
|
|
|
|
| 182 |
|
| 183 |
current_active_user = fastapi_users.current_user(active=True, optional=True)
|
| 184 |
|
|
|
|
| 185 |
def get_auth_router(app: FastAPI):
|
| 186 |
app.include_router(google_oauth_router, prefix="/auth/google", tags=["auth"])
|
| 187 |
app.include_router(github_oauth_router, prefix="/auth/github", tags=["auth"])
|
|
|
|
| 189 |
app.include_router(fastapi_users.get_register_router(UserRead, UserCreate), prefix="/auth", tags=["auth"])
|
| 190 |
app.include_router(fastapi_users.get_reset_password_router(), prefix="/auth", tags=["auth"])
|
| 191 |
app.include_router(fastapi_users.get_verify_router(UserRead), prefix="/auth", tags=["auth"])
|
| 192 |
+
app.include_router(fastapi_users.get_users_router(UserRead, UserUpdate), prefix="/users", tags=["users"])
|
main.py
CHANGED
|
@@ -118,6 +118,7 @@ app.add_middleware(
|
|
| 118 |
"https://mgzon-mgzon-app.hf.space",
|
| 119 |
"http://localhost:7860",
|
| 120 |
"http://localhost:8000",
|
|
|
|
| 121 |
"https://mgzon-mgzon-app.hf.space/auth/google/callback",
|
| 122 |
"https://mgzon-mgzon-app.hf.space/auth/github/callback",
|
| 123 |
],
|
|
@@ -132,6 +133,12 @@ app.include_router(api_router)
|
|
| 132 |
get_auth_router(app)
|
| 133 |
logger.debug("API and auth routers included")
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
# Add logout endpoint
|
| 136 |
@app.get("/logout")
|
| 137 |
async def logout(request: Request):
|
|
@@ -402,4 +409,4 @@ async def health_check():
|
|
| 402 |
|
| 403 |
if __name__ == "__main__":
|
| 404 |
logger.info(f"Starting uvicorn server on port {os.getenv('PORT', 7860)}")
|
| 405 |
-
uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 7860)))
|
|
|
|
| 118 |
"https://mgzon-mgzon-app.hf.space",
|
| 119 |
"http://localhost:7860",
|
| 120 |
"http://localhost:8000",
|
| 121 |
+
"https://hager-zon.vercel.app",
|
| 122 |
"https://mgzon-mgzon-app.hf.space/auth/google/callback",
|
| 123 |
"https://mgzon-mgzon-app.hf.space/auth/github/callback",
|
| 124 |
],
|
|
|
|
| 133 |
get_auth_router(app)
|
| 134 |
logger.debug("API and auth routers included")
|
| 135 |
|
| 136 |
+
# Add check-auth endpoint
|
| 137 |
+
@app.get("/api/check-auth")
|
| 138 |
+
async def check_auth(user: User = Depends(current_active_user)):
|
| 139 |
+
logger.debug(f"Checking auth for user: {user.email if user else 'Anonymous'}")
|
| 140 |
+
return {"is_authenticated": user is not None, "email": user.email if user else None}
|
| 141 |
+
|
| 142 |
# Add logout endpoint
|
| 143 |
@app.get("/logout")
|
| 144 |
async def logout(request: Request):
|
|
|
|
| 409 |
|
| 410 |
if __name__ == "__main__":
|
| 411 |
logger.info(f"Starting uvicorn server on port {os.getenv('PORT', 7860)}")
|
| 412 |
+
uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 7860)))
|
templates/login.html
CHANGED
|
@@ -200,6 +200,24 @@
|
|
| 200 |
const googleLoginBtn = document.getElementById('googleLoginBtn');
|
| 201 |
const githubLoginBtn = document.getElementById('githubLoginBtn');
|
| 202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
// Handle email/password login
|
| 204 |
loginForm.addEventListener('submit', async (e) => {
|
| 205 |
e.preventDefault();
|
|
@@ -294,7 +312,8 @@
|
|
| 294 |
|
| 295 |
// Check for error query parameters on page load (for OAuth errors)
|
| 296 |
window.addEventListener('load', () => {
|
| 297 |
-
console.log('Page loaded, checking
|
|
|
|
| 298 |
const urlParams = new URLSearchParams(window.location.search);
|
| 299 |
const error = urlParams.get('error');
|
| 300 |
if (error) {
|
|
@@ -349,4 +368,4 @@
|
|
| 349 |
</script>
|
| 350 |
|
| 351 |
</body>
|
| 352 |
-
</html>
|
|
|
|
| 200 |
const googleLoginBtn = document.getElementById('googleLoginBtn');
|
| 201 |
const githubLoginBtn = document.getElementById('githubLoginBtn');
|
| 202 |
|
| 203 |
+
// Check authentication status on page load
|
| 204 |
+
async function checkAuthStatus() {
|
| 205 |
+
try {
|
| 206 |
+
const response = await fetch('/api/check-auth', {
|
| 207 |
+
method: 'GET',
|
| 208 |
+
credentials: 'include',
|
| 209 |
+
headers: { 'Accept': 'application/json' }
|
| 210 |
+
});
|
| 211 |
+
const data = await response.json();
|
| 212 |
+
if (data.is_authenticated) {
|
| 213 |
+
console.log('User is authenticated, redirecting to /chat');
|
| 214 |
+
window.location.href = '/chat';
|
| 215 |
+
}
|
| 216 |
+
} catch (error) {
|
| 217 |
+
console.error('Error checking auth status:', error);
|
| 218 |
+
}
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
// Handle email/password login
|
| 222 |
loginForm.addEventListener('submit', async (e) => {
|
| 223 |
e.preventDefault();
|
|
|
|
| 312 |
|
| 313 |
// Check for error query parameters on page load (for OAuth errors)
|
| 314 |
window.addEventListener('load', () => {
|
| 315 |
+
console.log('Page loaded, checking auth status');
|
| 316 |
+
checkAuthStatus();
|
| 317 |
const urlParams = new URLSearchParams(window.location.search);
|
| 318 |
const error = urlParams.get('error');
|
| 319 |
if (error) {
|
|
|
|
| 368 |
</script>
|
| 369 |
|
| 370 |
</body>
|
| 371 |
+
</html>
|