Mark-Lasfar commited on
Commit
e7ce5ff
·
1 Parent(s): 10ac3d0

update main.py

Browse files
Files changed (2) hide show
  1. api/auth.py +5 -14
  2. main.py +1 -7
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,10 +110,6 @@ 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
- 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,10 +123,6 @@ class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
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,15 +138,13 @@ class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
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,6 +172,7 @@ fastapi_users = FastAPIUsers[User, int](
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,4 +180,4 @@ def get_auth_router(app: FastAPI):
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"])
 
17
  import logging
18
  import secrets
19
 
20
+ from api.database import User, OAuthAccount, CustomSQLAlchemyUserDatabase, get_user_db # استيراد من database.py
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
  return user
114
  else:
115
  logger.error(f"No user found for OAuth account with user_id: {existing_oauth_account.user_id}")
 
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
  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
 
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
  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"])
main.py CHANGED
@@ -133,12 +133,6 @@ app.include_router(api_router)
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,4 +403,4 @@ async def health_check():
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)))
 
133
  get_auth_router(app)
134
  logger.debug("API and auth routers included")
135
 
 
 
 
 
 
 
136
  # Add logout endpoint
137
  @app.get("/logout")
138
  async def logout(request: Request):
 
403
 
404
  if __name__ == "__main__":
405
  logger.info(f"Starting uvicorn server on port {os.getenv('PORT', 7860)}")
406
+ uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 7860)))