Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raise AuthRedirect is not working #319

Open
ITWeirdRunner opened this issue May 23, 2024 · 3 comments
Open

raise AuthRedirect is not working #319

ITWeirdRunner opened this issue May 23, 2024 · 3 comments

Comments

@ITWeirdRunner
Copy link

raise AuthRedirect('/auth/login/password') is not working when I use it like in demo
Traceback (most recent call last): File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 407, in run_asgi result = await app( # type: ignore[func-returns-value] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 69, in __call__ return await self.app(scope, receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in __call__ await super().__call__(scope, receive, send) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/applications.py", line 123, in __call__ await self.middleware_stack(scope, receive, send) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__ raise exc File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__ await self.app(scope, receive, _send) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 65, in __call__ await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app raise exc File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/routing.py", line 756, in __call__ await self.middleware_stack(scope, receive, send) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/routing.py", line 776, in app await route.handle(scope, receive, send) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/routing.py", line 297, in handle await self.app(scope, receive, send) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/routing.py", line 77, in app await wrap_app_handling_exceptions(app, request)(scope, receive, send) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app raise exc File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/routing.py", line 72, in app response = await func(request) ^^^^^^^^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/fastapi/routing.py", line 269, in app solved_result = await solve_dependencies( ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/fastapi/dependencies/utils.py", line 602, in solve_dependencies solved = await run_in_threadpool(call, **sub_values) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/concurrency.py", line 42, in run_in_threadpool return await anyio.to_thread.run_sync(func, *args) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/anyio/to_thread.py", line 56, in run_sync return await get_async_backend().run_sync_in_worker_thread( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 2144, in run_sync_in_worker_thread return await future ^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 851, in run result = context.run(func, *args) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/src/auth_user.py", line 28, in from_request raise AuthRedirect('/auth/login/password') fastui.auth.shared.AuthRedirect: Auth redirect to /auth/login/passwordINFO: 127.0.0.1:59513 - "GET /api/auth/profile HTTP/1.1" 500 Internal Server Error ERROR: Exception in ASGI application Traceback (most recent call last): File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 407, in run_asgi result = await app( # type: ignore[func-returns-value] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 69, in __call__ return await self.app(scope, receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in __call__ await super().__call__(scope, receive, send) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/applications.py", line 123, in __call__ await self.middleware_stack(scope, receive, send) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__ raise exc File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__ await self.app(scope, receive, _send) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 65, in __call__ await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app raise exc File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/routing.py", line 756, in __call__ await self.middleware_stack(scope, receive, send) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/routing.py", line 776, in app await route.handle(scope, receive, send) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/routing.py", line 297, in handle await self.app(scope, receive, send) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/routing.py", line 77, in app await wrap_app_handling_exceptions(app, request)(scope, receive, send) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app raise exc File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/routing.py", line 72, in app response = await func(request) ^^^^^^^^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/fastapi/routing.py", line 269, in app solved_result = await solve_dependencies( ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/fastapi/dependencies/utils.py", line 602, in solve_dependencies solved = await run_in_threadpool(call, **sub_values) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/starlette/concurrency.py", line 42, in run_in_threadpool return await anyio.to_thread.run_sync(func, *args) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/anyio/to_thread.py", line 56, in run_sync return await get_async_backend().run_sync_in_worker_thread( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 2144, in run_sync_in_worker_thread return await future ^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 851, in run result = context.run(func, *args) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/opensource/PycharmProjects/be_happy.intefrace/src/auth_user.py", line 28, in from_request raise AuthRedirect('/auth/login/password') fastui.auth.shared.AuthRedirect: Auth redirect to/auth/login/password

@shroominic
Copy link

same for me

@satan1437
Copy link

You need to register this func in your app:

app = FastAPI()
fastapi_auth_exception_handling(app)
# fastui/auth/shared.py
def fastapi_auth_exception_handling(app: 'FastAPI') -> None:
    """
    Register an exception handler for any `AuthException` in a FastAPI app.
    """
    from fastapi import Request, Response

    @app.exception_handler(AuthException)
    def auth_exception_handler(_request: Request, e: AuthException) -> Response:
        status_code, body = e.response_data()
        return Response(body, media_type='application/json', status_code=status_code)

@ITWeirdRunner
Copy link
Author

You need to register this func in your app:

app = FastAPI()
fastapi_auth_exception_handling(app)
# fastui/auth/shared.py
def fastapi_auth_exception_handling(app: 'FastAPI') -> None:
   """
   Register an exception handler for any `AuthException` in a FastAPI app.
   """
   from fastapi import Request, Response

   @app.exception_handler(AuthException)
   def auth_exception_handler(_request: Request, e: AuthException) -> Response:
       status_code, body = e.response_data()
       return Response(body, media_type='application/json', status_code=status_code)

thank you. I will try
where I can read about this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants