1414from __future__ import annotations
1515
1616from os import environ
17- from typing import TYPE_CHECKING , Any , Awaitable , Callable , Dict , Union
17+ from typing import Any , Dict
1818
1919from httpx import AsyncClient
2020
2828from supertokens_python .supertokens import AppInfo
2929from supertokens_python .utils import handle_httpx_client_exceptions
3030
31- if TYPE_CHECKING :
32- from supertokens_python .recipe .emailpassword .utils import (
33- InputResetPasswordUsingTokenFeature ,
34- )
3531
32+ async def create_and_send_email_using_supertokens_service (
33+ app_info : AppInfo , user : User , password_reset_url_with_token : str
34+ ) -> None :
35+ if ("SUPERTOKENS_ENV" in environ ) and (environ ["SUPERTOKENS_ENV" ] == "testing" ):
36+ return
3637
37- def default_create_and_send_custom_email (
38- app_info : AppInfo ,
39- ) -> Callable [[User , str , Dict [str , Any ]], Awaitable [None ]]:
40- async def func (user : User , password_reset_url_with_token : str , _ : Dict [str , Any ]):
41- if ("SUPERTOKENS_ENV" in environ ) and (environ ["SUPERTOKENS_ENV" ] == "testing" ):
42- return
43- data = {
44- "email" : user .email ,
45- "appName" : app_info .app_name ,
46- "passwordResetURL" : password_reset_url_with_token ,
47- }
48- try :
49- async with AsyncClient () as client :
50- resp = await client .post ("https://api.supertokens.io/0/st/auth/password/reset" , json = data , headers = {"api-version" : "0" }) # type: ignore
51- resp .raise_for_status ()
52- log_debug_message ("Password reset email sent to %s" , user .email )
53- except Exception as e :
54- log_debug_message ("Error sending password reset email" )
55- handle_httpx_client_exceptions (e , data )
56-
57- return func
38+ data = {
39+ "email" : user .email ,
40+ "appName" : app_info .app_name ,
41+ "passwordResetURL" : password_reset_url_with_token ,
42+ }
43+ try :
44+ async with AsyncClient () as client :
45+ resp = await client .post ("https://api.supertokens.io/0/st/auth/password/reset" , json = data , headers = {"api-version" : "0" }) # type: ignore
46+ resp .raise_for_status ()
47+ log_debug_message ("Password reset email sent to %s" , user .email )
48+ except Exception as e :
49+ log_debug_message ("Error sending password reset email" )
50+ handle_httpx_client_exceptions (e , data )
5851
5952
6053class BackwardCompatibilityService (EmailDeliveryInterface [EmailTemplateVars ]):
@@ -64,27 +57,9 @@ def __init__(
6457 self ,
6558 app_info : AppInfo ,
6659 recipe_interface_impl : RecipeInterface ,
67- reset_password_using_token_feature : Union [
68- InputResetPasswordUsingTokenFeature , None
69- ] = None ,
7060 ) -> None :
7161 self .recipe_interface_impl = recipe_interface_impl
72-
73- reset_password_feature_send_email_func = default_create_and_send_custom_email (
74- app_info
75- )
76- if (
77- reset_password_using_token_feature
78- and reset_password_using_token_feature .create_and_send_custom_email
79- is not None
80- ):
81- reset_password_feature_send_email_func = (
82- reset_password_using_token_feature .create_and_send_custom_email
83- )
84-
85- self .reset_password_feature_send_email_func = (
86- reset_password_feature_send_email_func
87- )
62+ self .app_info = app_info
8863
8964 async def send_email (
9065 self ,
@@ -102,8 +77,8 @@ async def send_email(
10277 # will get reset by the getUserById call above.
10378 user .email = template_vars .user .email
10479 try :
105- await self . reset_password_feature_send_email_func (
106- user , template_vars .password_reset_link , user_context
80+ await create_and_send_email_using_supertokens_service (
81+ self . app_info , user , template_vars .password_reset_link
10782 )
10883 except Exception :
10984 pass
0 commit comments