Skip to content

Commit

Permalink
Better CSRF method for htmx
Browse files Browse the repository at this point in the history
  • Loading branch information
FroMage committed May 14, 2024
1 parent 6b73950 commit d03d71f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/main/resources/templates/Todos/htmx.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
{#if todo.done}
<button type="submit" class="btn btn-warning" title="{m:todos.index.undone}"
hx-post="{uri:Todos.done(todo.id)}" hx-target="#row-{todo.id}" hx-swap="outerHTML"
hx-vals='{"{inject:csrf.parameterName}": "{inject:csrf.token}"}'><i class="bi-arrow-counterclockwise"></i></button>
><i class="bi-arrow-counterclockwise"></i></button>
{#else}
<button type="submit" class="btn btn-success" title="{m:todos.index.done}"
hx-post="{uri:Todos.done(todo.id)}" hx-target="#row-{todo.id}" hx-swap="outerHTML"
hx-vals='{"{inject:csrf.parameterName}": "{inject:csrf.token}"}'><i class="bi-check"></i></button>
><i class="bi-check"></i></button>
{/if}
<button type="submit" class="btn btn-danger" title="{m:todos.index.delete}"
hx-post="{uri:Todos.delete(todo.id)}" hx-target="#row-{todo.id}" hx-swap="delete"
hx-vals='{"{inject:csrf.parameterName}": "{inject:csrf.token}"}'><i class="bi-trash"></i></button>
><i class="bi-trash"></i></button>
</td>
</tr>
{/fragment}
Expand All @@ -52,8 +52,7 @@
<th scope="row">{m:todos.index.new}</th>
<td>
<input hx-post="{uri:Todos.add()}" name="task" placeholder="{m:todos.index.placeholder}" autofocus
hx-target="#new" hx-swap="beforebegin"
hx-vals='{"{inject:csrf.parameterName}": "{inject:csrf.token}"}'/>
hx-target="#new" hx-swap="beforebegin"/>
</td>
<td></td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<link rel="shortcut icon" type="image/png" href="/static/images/favicon.svg"/>
{#insert moreScripts /}
</head>
<body>
<body hx-headers='{"{inject:csrf.headerName}":"{inject:csrf.token}"}'>
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-4">
<div class="container-fluid">
<a class="navbar-brand" href="{uri:Application.index()}">Todo</a>
Expand Down

0 comments on commit d03d71f

Please sign in to comment.