1616 send_confirmation_email ,
1717 send_change_password_confirmation ,
1818 send_changes_password_notification ,
19+ send_reset_email ,
1920)
2021from .auth_method import AuthMethodMeta , Session
2122from fastapi .background import BackgroundTasks
@@ -133,7 +134,7 @@ async def _login(user_inp: EmailLogin) -> Session:
133134 ):
134135 raise AuthFailed (error = "Incorrect login or password" )
135136 db .session .add (user_session := UserSession (user_id = query .user .id , token = random_string ()))
136- db .session .flush ()
137+ db .session .commit ()
137138 return Session (
138139 user_id = user_session .user_id , token = user_session .token , id = user_session .id , expires = user_session .expires
139140 )
@@ -155,13 +156,14 @@ async def _add_to_db(user_inp: EmailRegister, confirmation_token: str, user: Use
155156 )
156157 db .session .flush ()
157158
159+
158160 @staticmethod
159161 async def _change_confirmation_link (user : User , confirmation_token : str ) -> None :
160162 if user .auth_methods .confirmed .value == "true" :
161163 raise AlreadyExists (User , user .id )
162164 else :
163165 user .auth_methods .confirmation_token .value = confirmation_token
164- db . session . flush ()
166+
165167
166168 @staticmethod
167169 async def _get_user_by_token_and_id (id : int , token : str ) -> User :
@@ -198,6 +200,7 @@ async def _register(
198200 to_addr = user_inp .email ,
199201 link = f"{ settings .APPLICATION_HOST } /email/approve?token={ confirmation_token } " ,
200202 )
203+ db .session .commit ()
201204 return ResponseModel (status = "Success" , message = "Email confirmation link sent" )
202205 if user_inp .user_id and token :
203206 user = await Email ._get_user_by_token_and_id (user_inp .user_id , token )
@@ -211,6 +214,7 @@ async def _register(
211214 to_addr = user_inp .email ,
212215 link = f"{ settings .APPLICATION_HOST } /email/approve?token={ confirmation_token } " ,
213216 )
217+ db .session .commit ()
214218 raise HTTPException (
215219 status_code = 201 , detail = ResponseModel (status = "Success" , message = "Email confirmation link sent" ).json ()
216220 )
@@ -238,8 +242,8 @@ async def _approve_email(token: str) -> ResponseModel:
238242 )
239243 if not auth_method :
240244 raise HTTPException (status_code = 403 , detail = ResponseModel (status = "Error" , message = "Incorrect link" ).json ())
241- auth_method .user .auth_methods .confirmed .value = True
242- db .session .flush ()
245+ auth_method .user .auth_methods .confirmed .value = "true"
246+ db .session .commit ()
243247 return ResponseModel (status = "Success" , message = "Email approved" )
244248
245249 @staticmethod
@@ -269,12 +273,12 @@ async def _request_reset_email(
269273 user_id = session .user_id , auth_method = Email .get_name (), param = "tmp_email_confirmation_token" , value = token
270274 )
271275 db .session .add_all ([tmp_email , tmp_email_confirmation_token ])
272- db .session .flush ()
273276 background_tasks .add_task (
274- send_confirmation_email ,
277+ send_reset_email ,
275278 to_addr = scheme .email ,
276279 link = f"{ settings .APPLICATION_HOST } /email/reset/email/{ session .user_id } ?token={ token } &email={ scheme .email } " ,
277280 )
281+ db .session .commit ()
278282 return ResponseModel (status = "Success" , message = "Email confirmation link sent" )
279283
280284 @staticmethod
@@ -295,7 +299,7 @@ async def _reset_email(user_id: int, token: str, email: str):
295299 user .auth_methods .email .value = user .auth_methods .tmp_email .value
296300 db .session .delete (user .auth_methods .tmp_email_confirmation_token )
297301 db .session .delete (user .auth_methods .tmp_email )
298- db .session .flush ()
302+ db .session .commit ()
299303 return ResponseModel (status = "Success" , message = "Email successfully changed" )
300304
301305 @staticmethod
@@ -326,8 +330,8 @@ async def _request_reset_password(
326330 )
327331 session .user .auth_methods .hashed_password .value = Email ._hash_password (schema .new_password , salt )
328332 session .user .auth_methods .salt .value = salt
329- db .session .flush ()
330333 background_tasks .add_task (send_changes_password_notification , session .user .auth_methods .email .value )
334+ db .session .commit ()
331335 return ResponseModel (status = "Success" , message = "Password has been successfully changed" )
332336 elif not token and not schema .password and not schema .new_password :
333337 user : User = db .session .query (User ).get (user_id )
@@ -345,7 +349,7 @@ async def _request_reset_password(
345349 db .session .add (
346350 AuthMethod (user_id = user_id , auth_method = Email .get_name (), param = "reset_token" , value = random_string ())
347351 )
348- db .session .flush ()
352+ db .session .commit ()
349353 background_tasks .add_task (
350354 send_change_password_confirmation ,
351355 user .auth_methods .email .value ,
@@ -372,5 +376,5 @@ async def _reset_password(
372376 user .auth_methods .hashed_password .value = Email ._hash_password (schema .new_password , salt )
373377 user .auth_methods .salt .value = salt
374378 db .session .delete (user .auth_methods .reset_token )
375- db .session .flush ()
379+ db .session .commit ()
376380 return ResponseModel (status = "Success" , message = "Password has been successfully changed" )
0 commit comments