Protect a WebSocket #62
-
In FastAPI-Users it is not as easy to protect a WebSocket as an HTTP endpoint (see this issue). @app.websocket("/protected", name="protected")
async def protected(
websocket: WebSocket,
user: FiefUserInfo = Depends(auth.current_user()),
):
... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Unfortunately, we hit the same limitations around security dependencies and WebSockets: FastAPI assume an HTTP The only solution right now would be to:
I think it would be interesting though to have it natively in the FastAPI integration; so I'll add it to my backlog 👍 |
Beta Was this translation helpful? Give feedback.
Unfortunately, we hit the same limitations around security dependencies and WebSockets: FastAPI assume an HTTP
Request
in those, so it won't work with a websocket. Besides, error responses are quite different with WebSockets.The only solution right now would be to:
WebSocket
object.validate_access_token
method on the Fief client.await websocket.close(code=status.WS_1008_POLICY_VIOLATION)
I think it would be interesting though to have it natively in the FastAPI integration; so I'll add it to my back…