| CARVIEW |
Select Language
HTTP/2 301
cache-status: "Netlify Edge"; fwd=miss
content-type: text/html
date: Sun, 28 Dec 2025 01:22:50 GMT
location: /examples/async-auth/
server: Netlify
strict-transport-security: max-age=31536000
x-nf-request-id: 01KDH8T43F62SX6VEYEJTP18FA
content-length: 98
HTTP/2 200
accept-ranges: bytes
age: 0
cache-control: public,max-age=0,must-revalidate
cache-status: "Netlify Edge"; fwd=miss
content-encoding: gzip
content-type: text/html; charset=UTF-8
date: Sun, 28 Dec 2025 01:22:51 GMT
etag: "433571d44f50036c9309579fa629fc4a-ssl-df"
server: Netlify
strict-transport-security: max-age=31536000
vary: Accept-Encoding
x-nf-request-id: 01KDH8T4G3Q0CRBV52HV5RBA52
</> htmx ~ Examples ~ Async Authentication
Async Authentication
This example shows how to implement an an async auth token flow for htmx.
The technique we will use here will take advantage of the fact that you can delay requests
using the htmx:confirm event.
We first have a button that should not issue a request until an auth token has been retrieved:
<button hx-post="/example" hx-target="next output">
An htmx-Powered button
</button>
<output>
--
</output>
Next we will add some scripting to work with an auth promise (returned by a library):
<script>
// auth is a promise returned by our authentication system
// await the auth token and store it somewhere
let authToken = null;
auth.then((token) => {
authToken = token
})
// gate htmx requests on the auth token
htmx.on("htmx:confirm", (e)=> {
// if there is no auth token
if(authToken == null) {
// stop the regular request from being issued
e.preventDefault()
// only issue it once the auth promise has resolved
auth.then(() => e.detail.issueRequest())
}
})
// add the auth token to the request as a header
htmx.on("htmx:configRequest", (e)=> {
e.detail.headers["AUTH"] = authToken
})
</script>
Here we use a global variable, but you could use localStorage or whatever preferred mechanism
you want to communicate the authentication token to the htmx:configRequest event.
With this code in place, htmx will not issue requests until the auth promise has been resolved.