codemation / easyauth

Create a centralized Authentication and Authorization token server. Easily secure FastAPI endpoints based on Users, Groups, Roles or Permissions with very little database usage.
https://easyauth.readthedocs.io/en/latest/
MIT License
553 stars 52 forks source link

raise RuntimeError("Cannot add middleware after an application has started") #98

Closed jonra1993 closed 1 year ago

jonra1993 commented 1 year ago

Thanks in advance for this tool, it is something I was looking for some time age. I was trying to initialize EasyAuthClient on new fastapi lifespan like this

@asynccontextmanager
async def lifespan(app: FastAPI):
    # Startup
    await EasyAuthClient.create(
        app,
        token_server='0.0.0.0',
        token_server_port=8090,
        auth_secret='abcd1234',
        default_permissions={'groups': ['users']}
    )
    print("startup fastapi")
    yield
    # shutdown

and I get this message error

image

Do you have any idea how to run EasyAuthClient in lifespan also there is a way to use client as depends?

codemation commented 1 year ago

Using client as Depends is not an option explicitly right now.

Creating a client is no longer required inside an event loop or supported after an FastAPI app has started due to changes in latest releases of FastAPI. See the latest example in docs and readme for latest usage. If you referenced the medium post, I still need to update that.

jonra1993 commented 1 year ago

Thanks for the information @codemation