[QUESTION] Returning a RedirectResponse in a dependency
See original GitHub issueDescription
How can I return a Redirect in a dependency to be executed in a conditional? In the example below, the code works, but I just get an exception. I want a redirect.:
def logged_in(user: models.User = Depends(current_user)):
if not user.is_authenticated:
raise HTTPException(status_code=303, detail="You need to login")
@users.get('/channel/{username}', dependencies=[Depends(logged_in)])
def channel(*, username: str,
request: Request,
current_user: models.User = Depends(current_user),
db: Session = Depends(get_db))
do_something()
Below is what I want, but when I use this dependency nothing happens because doesn’t use the return value. I can’t think of a way to do this without referencing the Dependency directly.
def logged_in(user: models.User = Depends(current_user)):
if not user.is_authenticated:
return RedirectResponse(url='/login')
@users.get('/channel/{username}', dependencies=[Depends(logged_in)])
def channel(*, username: str,
request: Request,
current_user: models.User = Depends(current_user),
db: Session = Depends(get_db))
do_something()
This works, but I have to put it in every single endpoint: @users.get(‘/channel/{username}’, dependencies=[Depends(logged_in)])
def channel(*, username: str,
request: Request,
current_user: models.User = Depends(current_user),
db: Session = Depends(get_db)):
if not user.is_authenticated:
return RedirectResponse(url='/login')
Issue Analytics
- State:
- Created 4 years ago
- Comments:13 (3 by maintainers)
Top Results From Across the Web
tiangolo/fastapi - Gitter
it returns a redirect response object to the endpoint - I want the dependency to force a redirect if the user isn't logged...
Read more >How to do a Post/Redirect/Get (PRG) in FastAPI?
I guess the RedirectResponse carries over the HTTP POST verb rather than becoming ... Implementation details ... return fastapi.responses.
Read more >Custom Response - HTML, Stream, File, others - FastAPI
RedirectResponse ¶ ... Returns an HTTP redirect. Uses a 307 status code (Temporary Redirect) by default. ... If you do that, then you...
Read more >Disabling Page Cache for Redirect Response - Drupal Answers
This doesn't exactly answer your question, but I feel it's relevant ... In your case you'd add cache dependencies relevant to determining ...
Read more >Event Dispatcher and redirects | Drupal 9 Module Development
return new \Symfony\Component\HttpFoundation\RedirectResponse('/node/1'); ... The dependency is actually the service that points to the current user (either ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I doubt if this works at all. At least the following does not work for me.
What I did before is to use custom exception handler:
Hi! Try it