adyus3380
09/20/2022, 5:23 PMresponse = await api_implementation.sign_up_post(
File \"/var/task/app/users/utils.py\", line 93, in sign_up_post
response = await original_sign_up_post(form_fields, api_options, user_context)
File \"/var/lang/lib/python3.9/site-packages/supertokens_python/recipe/emailpassword/api/implementation.py\", line 199, in sign_up_post
result = await api_options.recipe_implementation.sign_up(
File \"/var/lang/lib/python3.9/site-packages/supertokens_python/recipe/emailpassword/recipe_implementation.py\", line 123, in sign_up
response = await self.querier.send_post_request(
File \"/var/lang/lib/python3.9/site-packages/supertokens_python/querier.py\", line 167, in send_post_request
return await self.__send_request_helper(path, \"POST\", f, len(self.__hosts))
File \"/var/lang/lib/python3.9/site-packages/supertokens_python/querier.py\", line 246, in __send_request_helper
raise_general_exception(e)
File \"/var/lang/lib/python3.9/site-packages/supertokens_python/exceptions.py\", line 25, in raise_general_exception
raise GeneralError(msg) from None
supertokens_python.exceptions.GeneralError
Now, I've tried running the same FastAPI locally (sans Lambda), passing the same Supertokens instance and same API database in production (just the API running locally in uvicorn). I was able to successfully create a new user! Then, I was able to successfully sign in as that user via the frontend hitting the Lambda API π€― This tells me that the Lambda function does have access to the Supertokens deployment for signin functions, but mysteriously fails for signup.adyus3380
09/20/2022, 5:26 PMsupertokens_python/utils.py:174: RuntimeWarning: Inconsistent mode detected, check if you are using the right asgi / wsgi mode
I was unable to run the Lambda function for a long while, until I used https://github.com/erdewit/nest_asyncio to get around RuntimeError: no running event loop
issues
- the Lambda function is not in a VPC and does have general Internet accessadyus3380
09/20/2022, 5:27 PMGeneralError
means the POST request to Supertokens failed for some reason (ooh, I could check the Supertokens logs, BRB)rp_st
09/20/2022, 5:32 PMadyus3380
09/20/2022, 5:32 PM20 Sep 2022 17:22:58:758 +0000 | INFO | pid: e91bbb61-5c6a-4188-877a-d95d648640f6 | [http-nio-0.0.0.0-3567-exec-2] thread | io.supertokens.webserver.WebserverAPI.service(WebserverAPI.java:184) | API ended: /recipe/signup. Method: POST
adyus3380
09/20/2022, 5:37 PMrp_st
09/20/2022, 5:45 PMrp_st
09/21/2022, 8:14 AMadyus3380
09/21/2022, 11:51 AMKShivendu
09/22/2022, 8:49 AMKShivendu
09/22/2022, 8:49 AMpython
from fastapi import FastAPI, Depends
from starlette.middleware.cors import CORSMiddleware
from mangum import Mangum
import nest_asyncio
nest_asyncio.apply()
from supertokens_python import init, SupertokensConfig, InputAppInfo
from supertokens_python.framework.fastapi import get_middleware
from supertokens_python.recipe.session import SessionContainer
from supertokens_python.recipe.session.framework.fastapi import verify_session
from supertokens_python.recipe import session, emailpassword
init(
supertokens_config=SupertokensConfig(
connection_uri="XXX",
api_key="YYY",
),
app_info=InputAppInfo(
app_name="SuperTokens Demo",
api_domain="http://localhost:8000",
website_domain="http://localhost:3000",
api_base_path="/auth",
),
framework="fastapi",
recipe_list=[
session.init(),
emailpassword.init(),
],
telemetry=False,
mode="wsgi",
)
app = FastAPI(title="MyAwesomeApp")
app.add_middleware(get_middleware())
@app.get("/hello")
def hello_world():
return {"message": "Hello World"}
@app.get("/secure")
def secure_api(s: SessionContainer = Depends(verify_session())):
user_id = s.user_id
return {"session": user_id}
handler = Mangum(app)
# To run locally: uvicorn lambda_function:app
KShivendu
09/22/2022, 8:51 AMKShivendu
09/22/2022, 8:52 AMadyus3380
09/22/2022, 9:34 AMnest_asyncio
to make it work). However, if/when I return to a Lambda deployment, is the nest_asyncio
still required, or will there be a fix for that, to make it work out-of-the-box on both uvicorn/other ASGI and Lambda?adyus3380
09/22/2022, 9:35 AMmode="wsgi"
Oh, I see what you did there. Is this required for Lambda use?adyus3380
09/22/2022, 9:47 AMKShivendu
09/22/2022, 10:02 AMadyus3380
09/22/2022, 10:10 AMInconsistent mode detected, check if you are using the right asgi / wsgi mode
adyus3380
09/22/2022, 10:11 AMasgi
mode that comes with the FastAPI middleware, plus nest_asyncio
to make it work with Mangum on LambdaKShivendu
09/22/2022, 10:16 AMKShivendu
09/22/2022, 10:17 AMmode="wsgi"
πadyus3380
09/22/2022, 10:20 AMmode="wsgi"
remove the need for nest_asyncio
at the top of the file? Would `await`s continue to work with Supertokens functions?KShivendu
09/22/2022, 10:27 AMKShivendu
09/22/2022, 11:09 AMnest_asyncio.apply()
- async route handlers aren't supported in the AWS Lambda environment (probably because Mangum can't handle them). But, async overrides for supertokens will work fine.
- Since AWS Lambda (magnum), doesn't support async route handlers, you should import from from supertokens_python.recipe.X.syncio
(not asyncio
)
- Supertokens-python is smart about wsgi/asgi part. But keep it wsgi
for AWS Lambda env.adyus3380
09/22/2022, 11:10 AMadyus3380
09/22/2022, 11:10 AMKShivendu
09/22/2022, 11:10 AMKShivendu
09/22/2022, 11:11 AMadyus3380
09/22/2022, 11:11 AMKShivendu
09/22/2022, 11:12 AMadyus3380
09/22/2022, 11:14 AMrp_st
09/22/2022, 11:14 AMrp_st
09/22/2022, 11:14 AMSuperTokens is an open source authentication solution offering features like: Different types of login: Email / password, Passwordless (OTP or Magic link based).
Powered by