1111# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1212# License for the specific language governing permissions and limitations
1313# under the License.
14- from typing import Union
14+ from typing import Union , cast
1515
1616from litestar import asgi
1717from litestar .config .app import AppConfig
@@ -31,9 +31,9 @@ def __init__(self, api_base_path: str = "/auth", app_root_path: str = ""):
3131 Initialize the SuperTokens plugin.
3232
3333 Args:
34- api_base_path: The base path for SuperTokens API routes (default: "/auth")
35- app_root_path: The root path of the Litestar app (e.g., "api/v1").
36- If provided, will be stripped from api_base_path for mounting.
34+ api_base_path: The base path for SuperTokens API routes (default: "/auth")
35+ app_root_path: The root path of the Litestar app (e.g., "api/v1").
36+ If provided, will be stripped from api_base_path for mounting.
3737 """
3838 self .full_api_base_path = api_base_path .rstrip ("/" )
3939 self .app_root_path = app_root_path .strip ("/" )
@@ -63,10 +63,10 @@ def on_app_init(self, app_config: AppConfig) -> AppConfig:
6363 Called during app initialization to register the SuperTokens ASGI app.
6464
6565 Args:
66- app_config: The Litestar application configuration
66+ app_config: The Litestar application configuration
6767
6868 Returns:
69- The modified application configuration
69+ The modified application configuration
7070 """
7171 from litestar import Request , Response
7272 from litestar .types import Receive , Scope , Send
@@ -92,9 +92,9 @@ async def supertokens_asgi_app(
9292 if scope ["type" ] != "http" :
9393 # Pass through non-HTTP requests
9494 not_found = Response (content = None , status_code = 404 )
95- await not_found .to_asgi_response (app = None , request = None )(
96- scope , receive , send
97- ) # type: ignore
95+ await not_found .to_asgi_response ( # type: ignore
96+ app = None , request = Request ( scope , receive , send )
97+ )( scope , receive , send )
9898 return
9999
100100 st = Supertokens .get_instance ()
@@ -110,16 +110,17 @@ async def supertokens_asgi_app(
110110 response = LitestarResponse (litestar_response )
111111
112112 # Let SuperTokens middleware handle the request
113- result : Union [LitestarResponse , None ] = await st .middleware (
114- custom_request , response , user_context
113+ result : Union [LitestarResponse , None ] = cast (
114+ LitestarResponse ,
115+ await st .middleware (custom_request , response , user_context ),
115116 )
116117
117118 if result is None :
118119 # Request was not handled by SuperTokens
119120 not_found_response = Response (content = None , status_code = 404 )
120- await not_found_response .to_asgi_response (app = None , request = None )(
121- scope , receive , send
122- ) # type: ignore
121+ await not_found_response .to_asgi_response (
122+ app = None , request = Request ( scope , receive , send )
123+ )( scope , receive , send ) # type: ignore
123124 return
124125
125126 # Handle session management
@@ -133,7 +134,7 @@ async def supertokens_asgi_app(
133134 # Send the response
134135 if isinstance (result , LitestarResponse ):
135136 asgi_response = result .response .to_asgi_response (
136- app = None , request = None
137+ app = None , request = Request ( scope , receive = receive , send = send )
137138 ) # type: ignore
138139 await asgi_response (scope , receive , send )
139140 return
@@ -142,8 +143,11 @@ async def supertokens_asgi_app(
142143 # Handle SuperTokens-specific errors
143144 error_response_obj = Response (content = None )
144145 error_response = LitestarResponse (error_response_obj )
145- result = await st .handle_supertokens_error (
146- custom_request , e , error_response , user_context
146+ result = cast (
147+ LitestarResponse ,
148+ await st .handle_supertokens_error (
149+ custom_request , e , error_response , user_context
150+ ),
147151 )
148152
149153 # Clear the session from request.state to prevent the middleware
@@ -153,16 +157,16 @@ async def supertokens_asgi_app(
153157
154158 if isinstance (result , LitestarResponse ):
155159 asgi_response = result .response .to_asgi_response (
156- app = None , request = None
160+ app = None , request = Request ( scope , receive , send )
157161 ) # type: ignore
158162 await asgi_response (scope , receive , send )
159163 return
160164
161165 # Fallback - this should not normally be reached
162166 fallback_response = Response (content = None , status_code = 500 )
163- await fallback_response .to_asgi_response (app = None , request = None )(
164- scope , receive , send
165- ) # type: ignore
167+ await fallback_response .to_asgi_response (
168+ app = None , request = Request ( scope , receive , send )
169+ )( scope , receive , send ) # type: ignore
166170
167171 # Mount the SuperTokens ASGI app to handle auth routes
168172 app_mount = asgi (self .api_base_path , is_mount = True )(supertokens_asgi_app )
@@ -178,24 +182,24 @@ def get_supertokens_plugin(
178182 Get a configured SuperTokens plugin for Litestar.
179183
180184 Args:
181- api_base_path: The base path for SuperTokens API routes (default: "/auth").
182- This should match the api_base_path in your SuperTokens init().
183- app_root_path: The root path of your Litestar app if using app path (e.g., "api/v1").
184- This will be automatically stripped from api_base_path for proper mounting.
185+ api_base_path: The base path for SuperTokens API routes (default: "/auth").
186+ This should match the api_base_path in your SuperTokens init().
187+ app_root_path: The root path of your Litestar app if using app path (e.g., "api/v1").
188+ This will be automatically stripped from api_base_path for proper mounting.
185189
186190 Returns:
187- A configured SupertokensPlugin instance
191+ A configured SupertokensPlugin instance
188192
189193 Example:
190- # Without app root path
191- app = Litestar(
192- plugins=[get_supertokens_plugin(api_base_path="/auth")]
193- )
194-
195- # With app root path
196- app = Litestar(
197- path="api/v1",
198- plugins=[get_supertokens_plugin(api_base_path="/api/v1/auth", app_root_path="api/v1")]
199- )
194+ # Without app root path
195+ app = Litestar(
196+ plugins=[get_supertokens_plugin(api_base_path="/auth")]
197+ )
198+
199+ # With app root path
200+ app = Litestar(
201+ path="api/v1",
202+ plugins=[get_supertokens_plugin(api_base_path="/api/v1/auth", app_root_path="api/v1")]
203+ )
200204 """
201205 return SupertokensPlugin (api_base_path = api_base_path , app_root_path = app_root_path )
0 commit comments