LonelyVikingMichael / litestar-users

Authentication and user management for the Litestar framework
https://lonelyvikingmichael.github.io/litestar-users/
MIT License
47 stars 12 forks source link

Custom SessionAuthMiddleware in settings #68

Open dshum opened 6 months ago

dshum commented 6 months ago

Since it isn't forbidden to specify custom auth_backend_class in LitestarUsersConfig, I'd like to redefine authentication_middleware_class with a custom middleware like this:

class CustomSessionAuthMiddleware(SessionAuthMiddleware):
    async def authenticate_request(self, connection: ASGIConnection[Any, Any, Any, Any]) -> AuthenticationResult:
        # custom logic here...
        return AuthenticationResult(user=user, auth=connection.session)

@dataclass
class MyCustomAppJWTAuth[User](JWTCookieAuth):
    authentication_middleware_class = CustomSessionAuthMiddleware

litestar_users_config = LitestarUsersConfig(
    auth_backend_class=MyCustomAppJWTAuth,
    ...

but litestar-users is still using the default one. I can add CustomSessionAuthMiddleware on the app layer, but thought that litestar-users itself could use it.