Skip to content

Commit 43a9fa1

Browse files
committed
refactor: Pass default user context for APIs in the framework middleware itself
1 parent 427a6b1 commit 43a9fa1

File tree

18 files changed

+55
-24
lines changed

18 files changed

+55
-24
lines changed

supertokens_python/framework/django/django_middleware.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
from asgiref.sync import async_to_sync
2020

21+
from supertokens_python.utils import default_user_context
22+
2123

2224
def middleware(get_response: Any):
2325
from supertokens_python import Supertokens
@@ -37,8 +39,10 @@ async def __asyncMiddleware(request: HttpRequest):
3739
from django.http import HttpResponse
3840

3941
response = DjangoResponse(HttpResponse())
42+
user_context = default_user_context(custom_request)
43+
4044
try:
41-
result = await st.middleware(custom_request, response)
45+
result = await st.middleware(custom_request, response, user_context)
4246
if result is None:
4347
result = await get_response(request)
4448
result = DjangoResponse(result)

supertokens_python/framework/fastapi/fastapi_middleware.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from typing import TYPE_CHECKING, Union
1717

1818
from supertokens_python.framework import BaseResponse
19+
from supertokens_python.utils import default_user_context
1920

2021
if TYPE_CHECKING:
2122
from fastapi import FastAPI, Request
@@ -46,8 +47,9 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint):
4647
try:
4748
custom_request = FastApiRequest(request)
4849
response = FastApiResponse(Response())
50+
user_context = default_user_context(custom_request)
4951
result: Union[BaseResponse, None] = await st.middleware(
50-
custom_request, response
52+
custom_request, response, user_context
5153
)
5254
if result is None:
5355
response = await call_next(request)

supertokens_python/framework/flask/flask_middleware.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from supertokens_python.async_to_sync_wrapper import sync
2020
from supertokens_python.framework import BaseResponse
21+
from supertokens_python.utils import default_user_context
2122

2223
if TYPE_CHECKING:
2324
from flask import Flask
@@ -50,8 +51,11 @@ def _():
5051

5152
request_ = FlaskRequest(request)
5253
response_ = FlaskResponse(Response())
54+
user_context = default_user_context(request_)
5355

54-
result: Union[BaseResponse, None] = sync(st.middleware(request_, response_))
56+
result: Union[BaseResponse, None] = sync(
57+
st.middleware(request_, response_, user_context)
58+
)
5559

5660
if result is not None:
5761
if isinstance(result, FlaskResponse):

supertokens_python/recipe/dashboard/recipe.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import re
1717
from os import environ
18-
from typing import TYPE_CHECKING, Awaitable, Callable, List, Optional, Union
18+
from typing import TYPE_CHECKING, Awaitable, Callable, List, Optional, Union, Dict, Any
1919

2020
from supertokens_python.normalised_url_path import NormalisedURLPath
2121
from supertokens_python.recipe_module import APIHandled, RecipeModule, ApiIdWithTenantId
@@ -133,6 +133,7 @@ async def handle_api_request(
133133
path: NormalisedURLPath,
134134
method: str,
135135
response: BaseResponse,
136+
user_context: Dict[str, Any],
136137
) -> Optional[BaseResponse]:
137138
api_options = APIOptions(
138139
request,
@@ -247,7 +248,7 @@ def reset():
247248
DashboardRecipe.__instance = None
248249

249250
def return_api_id_if_can_handle_request(
250-
self, path: NormalisedURLPath, method: str
251+
self, path: NormalisedURLPath, method: str, user_context: Dict[str, Any]
251252
) -> Union[ApiIdWithTenantId, None]:
252253
dashboard_bundle_path = self.app_info.api_base_path.append(
253254
NormalisedURLPath(DASHBOARD_API)

supertokens_python/recipe/emailpassword/recipe.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ async def handle_api_request(
175175
path: NormalisedURLPath,
176176
method: str,
177177
response: BaseResponse,
178+
user_context: Dict[str, Any],
178179
):
179180
api_options = APIOptions(
180181
request,

supertokens_python/recipe/emailverification/recipe.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ async def handle_api_request(
170170
path: NormalisedURLPath,
171171
method: str,
172172
response: BaseResponse,
173+
user_context: Dict[str, Any],
173174
) -> Union[BaseResponse, None]:
174175
api_options = APIOptions(
175176
request,

supertokens_python/recipe/jwt/recipe.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from __future__ import annotations
1515

1616
from os import environ
17-
from typing import TYPE_CHECKING, List, Union, Optional
17+
from typing import TYPE_CHECKING, List, Union, Optional, Dict, Any
1818

1919
from supertokens_python.querier import Querier
2020
from supertokens_python.recipe.jwt.api.implementation import APIImplementation
@@ -85,6 +85,7 @@ async def handle_api_request(
8585
path: NormalisedURLPath,
8686
method: str,
8787
response: BaseResponse,
88+
user_context: Dict[str, Any],
8889
):
8990
options = APIOptions(
9091
request,

supertokens_python/recipe/multitenancy/recipe.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ async def handle_api_request(
127127
path: NormalisedURLPath,
128128
method: str,
129129
response: BaseResponse,
130+
user_context: Dict[str, Any],
130131
) -> Union[BaseResponse, None]:
131132
api_options = APIOptions(
132133
request,

supertokens_python/recipe/openid/recipe.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from __future__ import annotations
1515

1616
from os import environ
17-
from typing import TYPE_CHECKING, List, Union, Optional
17+
from typing import TYPE_CHECKING, List, Union, Optional, Any, Dict
1818

1919
from supertokens_python.querier import Querier
2020
from supertokens_python.recipe.jwt import JWTRecipe
@@ -94,6 +94,7 @@ async def handle_api_request(
9494
path: NormalisedURLPath,
9595
method: str,
9696
response: BaseResponse,
97+
user_context: Dict[str, Any],
9798
):
9899
options = APIOptions(
99100
request,
@@ -108,7 +109,7 @@ async def handle_api_request(
108109
self.api_implementation, options
109110
)
110111
return await self.jwt_recipe.handle_api_request(
111-
request_id, tenant_id, request, path, method, response
112+
request_id, tenant_id, request, path, method, response, user_context
112113
)
113114

114115
async def handle_error(

supertokens_python/recipe/passwordless/recipe.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ async def handle_api_request(
190190
path: NormalisedURLPath,
191191
method: str,
192192
response: BaseResponse,
193+
user_context: Dict[str, Any],
193194
):
194195
options = APIOptions(
195196
request,

0 commit comments

Comments
 (0)