FastAPI login, question on setting cookie #27
Replies: 2 comments 1 reply
-
|
Thanks for the question Patrick! I will get back to you in the next couple of days. |
Beta Was this translation helpful? Give feedback.
-
|
Hi Patrick - I will try to provide some more details. For anyone else reading this, here is a link to the blog post in question: https://samedwardes.com/2022/04/14/fastapi-webapp-with-auth. In general, here is what happens:
# Set an HttpOnly cookie in the response. `httponly=True` prevents
# JavaScript from reading the cookie.
response.set_cookie(
key=settings.COOKIE_NAME,
value=f"Bearer {access_token}",
httponly=True
)
Hopefully this helps explain. When playing around with the app I suggest haveing dev tools open in your browser. In the network tab you can see the headers and cookies that are part of each request. In the application tab you can see any cookies that are set. |
Beta Was this translation helpful? Give feedback.



Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi Sam,
I have been learning how to set up a basic database and an interface with Fastapi. I've looked through a couple of different examples, including yours "How to create a FastAPI Web App with authentication".
I got it up and running, including login, with Openapi. Then I tried adding a simple web ui, also with Fastapi. The problemI ran into was setting the jwt cookie in the browser, during login. I found a lot of examples where they created a response from a dict and the did set.cookie and returned the response. This worked well for Openapi, but not for a browser. Then I tried your example and it worked perfectly (It sounds like a quick fix, but this took me working on and off on the problem for two weeks).
I still have some questions though.
Best regards,
Patrick
Beta Was this translation helpful? Give feedback.
All reactions