📒 docs: fix CSRF handler example (#3412)

docs(middleware/csrf): fix handler example
This commit is contained in:
Jason McNeil 2025-04-15 03:03:30 -03:00 committed by GitHub
parent 1a17500efc
commit 6eba33af25
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -48,25 +48,23 @@ Getting the CSRF token in a handler:
```go
func handler(c fiber.Ctx) error {
handler := csrf.HandlerFromContext(c)
// Get CSRF token from the context
token := csrf.TokenFromContext(c)
if handler == nil {
panic("csrf middleware handler not registered")
if token == "" {
return c.Status(fiber.StatusInternalServerError)
}
cfg := handler.Config
if cfg == nil {
panic("csrf middleware handler has no config")
}
if !strings.Contains(cfg.KeyLookup, ":") {
panic("invalid KeyLookup format")
}
formKey := strings.Split(cfg.KeyLookup, ":")[1]
// Note: Make sure this matches the KeyLookup configured in your middleware
// Example: If you configured csrf.Config{KeyLookup: "form:_csrf"}
formKey := "_csrf"
// Create a form with the CSRF token
tmpl := fmt.Sprintf(`<form action="/post" method="POST">
<input type="hidden" name="%s" value="%s">
<input type="text" name="message">
<input type="submit" value="Submit">
</form>`, formKey, token)
c.Set("Content-Type", "text/html")
return c.SendString(tmpl)
}