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

Add X-CSRFToken via htmx:configRequest event #196

Closed
valberg opened this issue Mar 1, 2022 · 3 comments
Closed

Add X-CSRFToken via htmx:configRequest event #196

valberg opened this issue Mar 1, 2022 · 3 comments

Comments

@valberg
Copy link

valberg commented Mar 1, 2022

Description

I wanted to hear if there is any interest in adding the X-CSRFToken to every HTMX request by grabbing the htmx:configRequest event?

I've had quite a good success by including the following in my base templates:

<script>
    // Make sure we have csrf_token in HTMX requests
    document.body.addEventListener('htmx:configRequest', (event) => {
        event.detail.headers['X-CSRFToken'] = '{{ csrf_token }}';
    })
</script>

Including this in django-htmx by either modifying django-htmx.js (and removing the non-debug guard in jinja.py) could be one way of doing it. Another could be a seperate {% htmx_csrf_header %} template tag.

Any thoughts?

@adamchainz
Copy link
Owner

There's no need for any Javascript. You can use hx-headers - this is already documented in the example project CSRF demo:

<section>
<p>
This form shows you how to implement CSRF with htmx, using <a href="https://dev.htmx.org/attributes/hx-headers/"> the<code>hx-headers</code> attribute</a>.
</p>
</section>
<section>
<!--
Note you can place the hx-headers attribute on your <body> tag so it
affects all requests, reducing repetition.
The header name may be changed with the CSRF_HEADER_NAME setting;
https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-CSRF_HEADER_NAME
Note we use {{ csrf_token }} instead of {% csrf_token %} to get the
variable rather than a rendered HTML <input>.
-->
<form hx-post="/csrf-demo/checker/"
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-target="#result">

I'm further adding a docs page in the Docs PR: https://github.com/adamchainz/django-htmx/pull/194/files#diff-3d3c9ddabd7ba7f0fa458351d17598c03548245af53febc5c09fd829bb42dea1

@valberg
Copy link
Author

valberg commented Mar 1, 2022

Oh yes I knew that :) But having to add that to each html tag where you want to do htmx requests "gets tedious". I started using hx-headers initially but kept on forgetting them, which is why I turned to the configRequest event - to remove friction :)

Anyway, I'll consider this as out-of-scope for django-htmx :)

@valberg
Copy link
Author

valberg commented Mar 1, 2022

Okay, my bad, I somehow totally missed the <body> part! That definitely makes my suggestion irrelevant! Thanks @adamchainz !

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

2 participants