Mark-Lasfar commited on
Commit ·
77b0d42
1
Parent(s): 3c58f9b
Fix ChunkedIteratorResult in SQLAlchemyUserDatabase and toggleBtn null error
Browse files- api/auth.py +20 -7
- api/database.py +4 -2
api/auth.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi_users import FastAPIUsers
|
| 2 |
from fastapi_users.authentication import CookieTransport, JWTStrategy, AuthenticationBackend
|
| 3 |
from fastapi_users.db import SQLAlchemyUserDatabase
|
|
@@ -52,7 +55,6 @@ GITHUB_REDIRECT_URL = os.getenv("GITHUB_REDIRECT_URL", "https://mgzon-mgzon-app.
|
|
| 52 |
google_oauth_client = GoogleOAuth2(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET)
|
| 53 |
github_oauth_client = GitHubOAuth2(GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET)
|
| 54 |
|
| 55 |
-
|
| 56 |
# قاعدة بيانات المستخدم
|
| 57 |
class CustomSQLAlchemyUserDatabase(SQLAlchemyUserDatabase):
|
| 58 |
async def get_by_email(self, email: str) -> Optional[User]:
|
|
@@ -69,7 +71,6 @@ class CustomSQLAlchemyUserDatabase(SQLAlchemyUserDatabase):
|
|
| 69 |
await self.session.refresh(user)
|
| 70 |
return user
|
| 71 |
|
| 72 |
-
|
| 73 |
# مدير المستخدمين
|
| 74 |
class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
|
| 75 |
reset_password_token_secret = SECRET
|
|
@@ -116,17 +117,31 @@ class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
|
|
| 116 |
|
| 117 |
existing_oauth_account = await self.get_by_oauth_account(oauth_name, account_id)
|
| 118 |
if existing_oauth_account:
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
if associate_by_email:
|
|
|
|
| 123 |
user = await self.user_db.get_by_email(account_email)
|
| 124 |
if user:
|
| 125 |
oauth_account.user_id = user.id
|
| 126 |
await self.add_oauth_account(oauth_account)
|
|
|
|
| 127 |
await self.on_after_login(user, request)
|
| 128 |
return user
|
| 129 |
|
|
|
|
| 130 |
user_dict = {
|
| 131 |
"email": account_email,
|
| 132 |
"hashed_password": self.password_helper.hash(secrets.token_hex(32)),
|
|
@@ -137,15 +152,14 @@ class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
|
|
| 137 |
user = await self.user_db.create(user_dict)
|
| 138 |
oauth_account.user_id = user.id
|
| 139 |
await self.add_oauth_account(oauth_account)
|
|
|
|
| 140 |
await self.on_after_login(user, request)
|
| 141 |
return user
|
| 142 |
|
| 143 |
-
|
| 144 |
# استدعاء user manager من get_user_db
|
| 145 |
async def get_user_manager(user_db: SQLAlchemyUserDatabase = Depends(get_user_db)):
|
| 146 |
yield UserManager(user_db)
|
| 147 |
|
| 148 |
-
|
| 149 |
# OAuth Routers
|
| 150 |
google_oauth_router = get_oauth_router(
|
| 151 |
google_oauth_client,
|
|
@@ -181,4 +195,3 @@ def get_auth_router(app: FastAPI):
|
|
| 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"])
|
| 184 |
-
|
|
|
|
| 1 |
+
# SPDX-FileCopyrightText: Hadad <hadad@linuxmail.org>
|
| 2 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 3 |
+
|
| 4 |
from fastapi_users import FastAPIUsers
|
| 5 |
from fastapi_users.authentication import CookieTransport, JWTStrategy, AuthenticationBackend
|
| 6 |
from fastapi_users.db import SQLAlchemyUserDatabase
|
|
|
|
| 55 |
google_oauth_client = GoogleOAuth2(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET)
|
| 56 |
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]:
|
|
|
|
| 71 |
await self.session.refresh(user)
|
| 72 |
return user
|
| 73 |
|
|
|
|
| 74 |
# مدير المستخدمين
|
| 75 |
class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
|
| 76 |
reset_password_token_secret = SECRET
|
|
|
|
| 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)
|
| 124 |
+
user = result.scalar_one_or_none()
|
| 125 |
+
|
| 126 |
+
if user:
|
| 127 |
+
logger.info(f"User found: {user.email}, proceeding with on_after_login")
|
| 128 |
+
await self.on_after_login(user, request)
|
| 129 |
+
return user
|
| 130 |
+
else:
|
| 131 |
+
logger.error(f"No user found for OAuth account with user_id: {existing_oauth_account.user_id}")
|
| 132 |
+
raise ValueError("User not found for existing OAuth account")
|
| 133 |
|
| 134 |
if associate_by_email:
|
| 135 |
+
logger.info(f"Associating OAuth account by email: {account_email}")
|
| 136 |
user = await self.user_db.get_by_email(account_email)
|
| 137 |
if user:
|
| 138 |
oauth_account.user_id = user.id
|
| 139 |
await self.add_oauth_account(oauth_account)
|
| 140 |
+
logger.info(f"User associated: {user.email}, proceeding with on_after_login")
|
| 141 |
await self.on_after_login(user, request)
|
| 142 |
return user
|
| 143 |
|
| 144 |
+
logger.info(f"Creating new user for email: {account_email}")
|
| 145 |
user_dict = {
|
| 146 |
"email": account_email,
|
| 147 |
"hashed_password": self.password_helper.hash(secrets.token_hex(32)),
|
|
|
|
| 152 |
user = await self.user_db.create(user_dict)
|
| 153 |
oauth_account.user_id = user.id
|
| 154 |
await self.add_oauth_account(oauth_account)
|
| 155 |
+
logger.info(f"New user created: {user.email}, proceeding with on_after_login")
|
| 156 |
await self.on_after_login(user, request)
|
| 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,
|
|
|
|
| 195 |
app.include_router(fastapi_users.get_reset_password_router(), prefix="/auth", tags=["auth"])
|
| 196 |
app.include_router(fastapi_users.get_verify_router(UserRead), prefix="/auth", tags=["auth"])
|
| 197 |
app.include_router(fastapi_users.get_users_router(UserRead, UserUpdate), prefix="/users", tags=["users"])
|
|
|
api/database.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
from datetime import datetime
|
|
@@ -52,7 +55,7 @@ class OAuthAccount(Base):
|
|
| 52 |
account_id = Column(String, index=True, nullable=False)
|
| 53 |
account_email = Column(String, nullable=False)
|
| 54 |
|
| 55 |
-
user = relationship("User", back_populates="oauth_accounts")
|
| 56 |
|
| 57 |
class User(SQLAlchemyBaseUserTable[int], Base):
|
| 58 |
__tablename__ = "user"
|
|
@@ -116,4 +119,3 @@ async def init_db():
|
|
| 116 |
except Exception as e:
|
| 117 |
logger.error(f"Error creating database tables: {e}")
|
| 118 |
raise
|
| 119 |
-
|
|
|
|
| 1 |
+
# SPDX-FileCopyrightText: Hadad <hadad@linuxmail.org>
|
| 2 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 3 |
+
|
| 4 |
import os
|
| 5 |
import logging
|
| 6 |
from datetime import datetime
|
|
|
|
| 55 |
account_id = Column(String, index=True, nullable=False)
|
| 56 |
account_email = Column(String, nullable=False)
|
| 57 |
|
| 58 |
+
user = relationship("User", back_populates="oauth_accounts", lazy="selectin")
|
| 59 |
|
| 60 |
class User(SQLAlchemyBaseUserTable[int], Base):
|
| 61 |
__tablename__ = "user"
|
|
|
|
| 119 |
except Exception as e:
|
| 120 |
logger.error(f"Error creating database tables: {e}")
|
| 121 |
raise
|
|
|