Mark-Lasfar commited on
Commit
06e9f8e
·
1 Parent(s): 6d7b4fd

Update Model

Browse files
Files changed (1) hide show
  1. api/auth.py +45 -19
api/auth.py CHANGED
@@ -78,16 +78,6 @@ class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
78
  logger.error(f"Invalid OAuth callback data: email={account_email}, account_id={account_id}")
79
  raise ValueError("Invalid OAuth callback data: email and account_id are required.")
80
 
81
- oauth_account_dict = {
82
- "oauth_name": oauth_name,
83
- "access_token": access_token,
84
- "account_id": account_id,
85
- "account_email": account_email,
86
- "expires_at": expires_at,
87
- "refresh_token": refresh_token,
88
- }
89
- oauth_account = OAuthAccount(**oauth_account_dict)
90
-
91
  try:
92
  # Custom query to fetch existing OAuth account (sync)
93
  statement = select(OAuthAccount).where(
@@ -125,7 +115,14 @@ class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
125
  else:
126
  # Update existing OAuth account if needed (handle None return from update_oauth_account)
127
  try:
128
- updated_user = self.user_db.update_oauth_account(user, existing_oauth_account, oauth_account_dict) # sync
 
 
 
 
 
 
 
129
  if updated_user is None:
130
  logger.warning("update_oauth_account returned None. Using original user.")
131
  updated_user = user
@@ -136,8 +133,8 @@ class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
136
  user = user # Keep original
137
  elif associate_by_email:
138
  logger.info(f"Associating by email: {account_email}")
139
- # Safe get_by_email (sync)
140
- user = self.user_db.get_by_email(account_email) # sync
141
  if user is None:
142
  logger.info(f"No user found for email {account_email}. Creating new user.")
143
  user_dict = {
@@ -147,13 +144,27 @@ class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
147
  "is_verified": is_verified_by_default,
148
  }
149
  try:
150
- user = self.user_db.create(user_dict) # sync
 
 
 
 
151
  logger.info(f"Created new user for email: {user.email}")
152
  except Exception as create_e:
 
153
  logger.error(f"Failed to create user for email {account_email}: {create_e}")
154
  raise ValueError(f"Failed to create user: {create_e}")
155
- # Link OAuth account
156
- oauth_account.user_id = user.id
 
 
 
 
 
 
 
 
 
157
  try:
158
  self.user_db.session.add(oauth_account)
159
  self.user_db.session.commit()
@@ -172,13 +183,27 @@ class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
172
  "is_verified": is_verified_by_default,
173
  }
174
  try:
175
- user = self.user_db.create(user_dict) # sync
 
 
 
 
176
  logger.info(f"Created new user: {user.email}")
177
  except Exception as create_e:
 
178
  logger.error(f"Failed to create user for email {account_email}: {create_e}")
179
  raise ValueError(f"Failed to create user: {create_e}")
180
 
181
- oauth_account.user_id = user.id
 
 
 
 
 
 
 
 
 
182
  try:
183
  self.user_db.session.add(oauth_account)
184
  self.user_db.session.commit()
@@ -197,9 +222,10 @@ class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
197
  logger.warning(f"User {user.email} is inactive. Activating...")
198
  user.is_active = True
199
  try:
200
- self.user_db.update(user) # sync
201
  logger.info(f"Activated inactive user: {user.email}")
202
  except Exception as activate_e:
 
203
  logger.error(f"Failed to activate user: {activate_e}")
204
  raise ValueError(f"Failed to activate user: {activate_e}")
205
 
 
78
  logger.error(f"Invalid OAuth callback data: email={account_email}, account_id={account_id}")
79
  raise ValueError("Invalid OAuth callback data: email and account_id are required.")
80
 
 
 
 
 
 
 
 
 
 
 
81
  try:
82
  # Custom query to fetch existing OAuth account (sync)
83
  statement = select(OAuthAccount).where(
 
115
  else:
116
  # Update existing OAuth account if needed (handle None return from update_oauth_account)
117
  try:
118
+ updated_user = self.user_db.update_oauth_account(user, existing_oauth_account, {
119
+ "oauth_name": oauth_name,
120
+ "access_token": access_token,
121
+ "account_id": account_id,
122
+ "account_email": account_email,
123
+ "expires_at": expires_at,
124
+ "refresh_token": refresh_token,
125
+ }) # sync
126
  if updated_user is None:
127
  logger.warning("update_oauth_account returned None. Using original user.")
128
  updated_user = user
 
133
  user = user # Keep original
134
  elif associate_by_email:
135
  logger.info(f"Associating by email: {account_email}")
136
+ # Safe get_by_email (sync) - NO AWAIT
137
+ user = self.user_db.get_by_email(account_email)
138
  if user is None:
139
  logger.info(f"No user found for email {account_email}. Creating new user.")
140
  user_dict = {
 
144
  "is_verified": is_verified_by_default,
145
  }
146
  try:
147
+ # Create user manually to avoid any async issues
148
+ user = User(**user_dict)
149
+ self.user_db.session.add(user)
150
+ self.user_db.session.commit()
151
+ self.user_db.session.refresh(user)
152
  logger.info(f"Created new user for email: {user.email}")
153
  except Exception as create_e:
154
+ self.user_db.session.rollback()
155
  logger.error(f"Failed to create user for email {account_email}: {create_e}")
156
  raise ValueError(f"Failed to create user: {create_e}")
157
+
158
+ # Create and link OAuth account
159
+ oauth_account = OAuthAccount(
160
+ oauth_name=oauth_name,
161
+ access_token=access_token,
162
+ account_id=account_id,
163
+ account_email=account_email,
164
+ expires_at=expires_at,
165
+ refresh_token=refresh_token,
166
+ user_id=user.id
167
+ )
168
  try:
169
  self.user_db.session.add(oauth_account)
170
  self.user_db.session.commit()
 
183
  "is_verified": is_verified_by_default,
184
  }
185
  try:
186
+ # Create user manually to avoid any async issues
187
+ user = User(**user_dict)
188
+ self.user_db.session.add(user)
189
+ self.user_db.session.commit()
190
+ self.user_db.session.refresh(user)
191
  logger.info(f"Created new user: {user.email}")
192
  except Exception as create_e:
193
+ self.user_db.session.rollback()
194
  logger.error(f"Failed to create user for email {account_email}: {create_e}")
195
  raise ValueError(f"Failed to create user: {create_e}")
196
 
197
+ # Create and link OAuth account
198
+ oauth_account = OAuthAccount(
199
+ oauth_name=oauth_name,
200
+ access_token=access_token,
201
+ account_id=account_id,
202
+ account_email=account_email,
203
+ expires_at=expires_at,
204
+ refresh_token=refresh_token,
205
+ user_id=user.id
206
+ )
207
  try:
208
  self.user_db.session.add(oauth_account)
209
  self.user_db.session.commit()
 
222
  logger.warning(f"User {user.email} is inactive. Activating...")
223
  user.is_active = True
224
  try:
225
+ self.user_db.session.commit()
226
  logger.info(f"Activated inactive user: {user.email}")
227
  except Exception as activate_e:
228
+ self.user_db.session.rollback()
229
  logger.error(f"Failed to activate user: {activate_e}")
230
  raise ValueError(f"Failed to activate user: {activate_e}")
231