Mark-Lasfar commited on
Commit
da514b1
·
1 Parent(s): 77b0d42

Fix ChunkedIteratorResult in SQLAlchemyUserDatabase and toggleBtn null error

Browse files
Files changed (1) hide show
  1. api/auth.py +10 -4
api/auth.py CHANGED
@@ -3,7 +3,7 @@
3
 
4
  from fastapi_users import FastAPIUsers
5
  from fastapi_users.authentication import CookieTransport, JWTStrategy, AuthenticationBackend
6
- from fastapi_users.db import SQLAlchemyUserDatabase
7
  from httpx_oauth.clients.google import GoogleOAuth2
8
  from httpx_oauth.clients.github import GitHubOAuth2
9
  from fastapi_users.router.oauth import get_oauth_router
@@ -57,6 +57,11 @@ github_oauth_client = GitHubOAuth2(GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET)
57
 
58
  # قاعدة بيانات المستخدم
59
  class CustomSQLAlchemyUserDatabase(SQLAlchemyUserDatabase):
 
 
 
 
 
60
  async def get_by_email(self, email: str) -> Optional[User]:
61
  logger.info(f"Checking for user with email: {email}")
62
  statement = select(self.user_table).where(self.user_table.email == email)
@@ -117,7 +122,6 @@ class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
117
 
118
  existing_oauth_account = await self.get_by_oauth_account(oauth_name, account_id)
119
  if existing_oauth_account:
120
- # جلب المستخدم يدويًا باستعلام async صريح
121
  logger.info(f"Fetching user for OAuth account with user_id: {existing_oauth_account.user_id}")
122
  statement = select(User).where(User.id == existing_oauth_account.user_id)
123
  result = await self.user_db.session.execute(statement)
@@ -157,10 +161,10 @@ class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
157
  return user
158
 
159
  # استدعاء user manager من get_user_db
160
- async def get_user_manager(user_db: SQLAlchemyUserDatabase = Depends(get_user_db)):
161
  yield UserManager(user_db)
162
 
163
- # OAuth Routers
164
  google_oauth_router = get_oauth_router(
165
  google_oauth_client,
166
  auth_backend,
@@ -170,6 +174,8 @@ google_oauth_router = get_oauth_router(
170
  redirect_url=GOOGLE_REDIRECT_URL,
171
  )
172
 
 
 
173
  github_oauth_router = get_oauth_router(
174
  github_oauth_client,
175
  auth_backend,
 
3
 
4
  from fastapi_users import FastAPIUsers
5
  from fastapi_users.authentication import CookieTransport, JWTStrategy, AuthenticationBackend
6
+ from fastapi_users.db import SQLAlchemyBaseUserTable, SQLAlchemyUserDatabase
7
  from httpx_oauth.clients.google import GoogleOAuth2
8
  from httpx_oauth.clients.github import GitHubOAuth2
9
  from fastapi_users.router.oauth import get_oauth_router
 
57
 
58
  # قاعدة بيانات المستخدم
59
  class CustomSQLAlchemyUserDatabase(SQLAlchemyUserDatabase):
60
+ def parse_id(self, value: Any) -> int:
61
+ """تحويل الـ ID من string إلى int لتوافق JWTStrategy"""
62
+ logger.debug(f"Parsing ID: {value} (type: {type(value)})")
63
+ return int(value) if isinstance(value, str) else value
64
+
65
  async def get_by_email(self, email: str) -> Optional[User]:
66
  logger.info(f"Checking for user with email: {email}")
67
  statement = select(self.user_table).where(self.user_table.email == email)
 
122
 
123
  existing_oauth_account = await self.get_by_oauth_account(oauth_name, account_id)
124
  if existing_oauth_account:
 
125
  logger.info(f"Fetching user for OAuth account with user_id: {existing_oauth_account.user_id}")
126
  statement = select(User).where(User.id == existing_oauth_account.user_id)
127
  result = await self.user_db.session.execute(statement)
 
161
  return user
162
 
163
  # استدعاء user manager من get_user_db
164
+ async def get_user_manager(user_db: CustomSQLAlchemyUserDatabase = Depends(get_user_db)):
165
  yield UserManager(user_db)
166
 
167
+ # OAuth Routers مع معالجة مخصصة لـ GitHub
168
  google_oauth_router = get_oauth_router(
169
  google_oauth_client,
170
  auth_backend,
 
174
  redirect_url=GOOGLE_REDIRECT_URL,
175
  )
176
 
177
+ github_oauth_client._access_token_url = "https://github.com/login/oauth/access_token"
178
+ github_oauth_client._access_token_params = {"headers": {"Accept": "application/json"}}
179
  github_oauth_router = get_oauth_router(
180
  github_oauth_client,
181
  auth_backend,