From b2447bc967df37b31282a97e32c581954bb8bcc9 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Thu, 17 Oct 2024 16:37:23 +0200 Subject: Move from html/template to templ --- csrf/csrf.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'csrf/csrf.go') 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 { -- cgit v1.2.3-70-g09d2