'FastAPI : pass session provider through @manager.user_loader
I'm confronted to an issue with fastapi_login. I'm trying to pass my db connection function to the manager user_loader.
I follow this doc : https://fastapi-login.readthedocs.io/advanced_usage/
database.py
def get_db():
db = SessionLocal()
try:
yield db
except:
db.close()
users.py
@manager.user_loader(session_provider=database.get_db)
async def get_user(username: str, session_provider) :
db = session_provider()
user = db.query(UserDb).filter_by(username=username).first()
return user
routes.py
@router.post('/login', response_model=UserSchema)
async def login(data: OAuth2PasswordRequestForm = Depends()):
username = data.username
password = data.password
user = await users.get_user(username)
It look like the session_provider is not catched by the @manager. I have the feeling that I respect what that said in the documentation. Here is the guvicorn error I get:
eb_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 366, in run_asgi
web_1 | result = await app(self.scope, self.receive, self.send)
web_1 | File "/usr/local/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
web_1 | return await self.app(scope, receive, send)
web_1 | File "/usr/local/lib/python3.9/site-packages/fastapi/applications.py", line 261, in __call__
web_1 | await super().__call__(scope, receive, send)
web_1 | File "/usr/local/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__
web_1 | await self.middleware_stack(scope, receive, send)
web_1 | File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
web_1 | raise exc
web_1 | File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
web_1 | await self.app(scope, receive, _send)
web_1 | File "/usr/local/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__
web_1 | raise exc
web_1 | File "/usr/local/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__
web_1 | await self.app(scope, receive, sender)
web_1 | File "/usr/local/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
web_1 | raise e
web_1 | File "/usr/local/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
web_1 | await self.app(scope, receive, send)
web_1 | File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 656, in __call__
web_1 | await route.handle(scope, receive, send)
web_1 | File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 259, in handle
web_1 | await self.app(scope, receive, send)
web_1 | File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 61, in app
web_1 | response = await func(request)
web_1 | File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 227, in app
web_1 | raw_response = await run_endpoint_function(
web_1 | File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 160, in run_endpoint_function
web_1 | return await dependant.call(**values)
web_1 | File "/code/app/routers/users.py", line 56, in login
web_1 | user = await lib.users.get_user(username)
web_1 | TypeError: get_user() missing 1 required positional argument: 'session_provider'
Any idea how to deal with this ?
Some references I tried without success :
Actually, I bypass the problem by sending the connection session as parameter of get_user in the route.But I prefer put my connection directly in @manager
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
