summaryrefslogtreecommitdiff
path: root/csrf
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--csrf/csrf.go17
1 files changed, 11 insertions, 6 deletions
diff --git a/csrf/csrf.go b/csrf/csrf.go
index 18fdb81..fd73c0d 100644
--- a/csrf/csrf.go
+++ b/csrf/csrf.go
@@ -4,6 +4,7 @@ import (
"html/template"
"net/http"
+ "github.com/a-h/templ"
"github.com/gorilla/csrf"
"github.com/gorilla/securecookie"
)
@@ -12,18 +13,22 @@ func Handler() func(http.Handler) http.Handler {
return csrf.Protect(
securecookie.GenerateRandomKey(32),
csrf.SameSite(csrf.SameSiteStrictMode),
- csrf.FieldName("csrf.csrffield"), // should match the structure in `Csrf`
+ csrf.FieldName("csrffield.field"), // should match the structure in `Csrf`
)
}
-// Csrf handles the CSRF data for a form.
+// CsrfField handles the CSRF data for a form.
// Include it verbatim and then use `{{.CsrfField}}` in templates.
-type Csrf struct {
- CsrfField template.HTML `form:"-" schema:"-"`
+type CsrfField struct {
+ field template.HTML `form:"-" schema:"-"`
}
-func (c *Csrf) SetCsrfField(r *http.Request) {
- c.CsrfField = csrf.TemplateField(r)
+func (c *CsrfField) SetCsrfField(r *http.Request) {
+ c.field = csrf.TemplateField(r)
+}
+
+func (c *CsrfField) Csrf() templ.Component {
+ return templ.Raw(c.field)
}
type Enabled interface {