-
Notifications
You must be signed in to change notification settings - Fork 9
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
Unable to access Response in the Route #8
Comments
Thanks for reporting the issue and for submitting a PR! |
Could you please provide example code for your use-case? |
I wanted to achieve the following:
Below is the route:
This code block coudn't set the cookies, but was able to return JSON / HTML respectively. I ended up removing the htmx decorator, and just return JSON for both web and API, and on the web side use client JS to catch 200 status code and then redirect user. |
Thanks for the example. The cookies you set on the response in the route will indeed be ignored, because the decorator creates and returns a I'll think about the right solution to this issue, in the meantime it might be best to return a |
Thanks Peter. What if we modify the Jinja class or the hx decorator to accept an additional argument that specifies the cookies to set? This requires changes at several levels but will make the decorator more flexible.
|
It's not that simple:
I'm thinking whether there is a nice and easy to use solution to this issue that doesn't require users to write too much custom code (a function that gets the template response, the route's return value, and the route's context). The Very simplified example: jinja = Jinja(Jinja2Templates("templates"))
def sign_in_renderer(result, *, context, request):
response = jinja.templates.TemplateResponse("sign_in_payload.html", context={}, request=request)
response.set_cookie("access_token", value=result.access_token)
response.set_cookie("refresh_token", value=result.refrest_token)
return response I did not mention it before, but with having this code in my own comment, I must warn you that (even accidentally) returning these tokens would be pretty dangerous, please avoid it. You can do that by setting |
Thanks Peter. My current solution is similar to your example. For this particular scenario it's probably easier to just writing two separate routes, I've wasted hours on this and I don't think it was worth it. 🤣 |
Sorry that I couldn't give you a better, more convenient solution (yet). I'll keep the issue open to keep this use-case (~authentication on HTMX routes). I've opened #10 to do some (necessary) convenience upgrades - maybe that will result in some improvements for this scenario as well. |
PR #11 may give you new options, but I think the discussed solution is the best one, so I hope the issue can be closed. |
See PR #19 for response header support. |
Problem statement: Unable to access Response in the route (e.g. setting cookies) as it's being handled by fasthx.
Currently using a workaround - hx-target to a hidden div for the returned JWT tokens, and use JS to detect the response code and redirect the browser.
Would be nice if we could access Response from within the route.
The text was updated successfully, but these errors were encountered: