Navbar logout as a POST form instead of a GET link
A GET logout can be triggered from any foreign page (img/link) and by link prefetchers. LogoutPath now renders <form method="post"> with a submit button styled like the former link; consumers register the route as POST only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b6a6298353
commit
d0e49b2365
4 files changed
+32
-6
No files matched your search
@@ -116,7 +116,7 @@ concurrent-edit conflict during the initial rollout) and can produce a
|
||||
force-move an existing tag again for any future change — always cut a
|
||||
new one, even for a one-line fix.
|
||||
|
||||
## PageHeader fields (current as of v0.1.9)
|
||||
## PageHeader fields (current as of v0.1.10)
|
||||
|
||||
`Title`, `AppName` (lowercase product name for `<title>`/footer — not
|
||||
`Name`, which collides with a domain type's own `Name` field once
|
||||
@@ -124,9 +124,13 @@ embedded in the same struct, as yakwui's viewmodel types do), `Lang`
|
||||
(`<html lang>`, defaults to `"en"`), `Theme` (`<html data-theme>`, see
|
||||
Theming), `Brand`, `BrandInitial`, `Subtitle`,
|
||||
`BasePath`, `NavItems`/`ActiveNav`, `Flash`/`FlashKind`, `Username`,
|
||||
`LogoutPath` (a plain GET link — for a POST-form logout or any other
|
||||
markup mismatch, render it yourself into `NavbarRightExtra` instead and
|
||||
leave `Username`/`LogoutPath` unset), `NavbarBeforeBrand` (before the
|
||||
`LogoutPath` (since v0.1.10 a `<form method="post">` with a submit
|
||||
button styled like the former link, never a GET link: a logout reachable
|
||||
by GET can be triggered from any foreign page via an `<img>`/link and by
|
||||
link prefetchers; the consumer registers the route as `POST` only and
|
||||
answers `GET` with 405 + `Allow: POST`. If the form needs a hidden CSRF
|
||||
field or other markup, render it yourself into `NavbarRightExtra` and
|
||||
leave `LogoutPath` unset), `NavbarBeforeBrand` (before the
|
||||
brand link, e.g. a mobile sidebar-toggle button),
|
||||
`NavbarLeftExtra`/`NavbarRightExtra` (the navbar's two groups),
|
||||
`ExtraHeadHTML`/`ExtraBodyHTML`, and `BodyAttrs`/`SidebarAttrs` —
|
||||
|
||||
@@ -75,7 +75,7 @@ type PageHeader struct {
|
||||
Flash string
|
||||
FlashKind string // "ok" or "error"; meaningless if Flash == ""
|
||||
Username string
|
||||
LogoutPath string // BasePath-relative, e.g. "/logout"; "" hides the logout link
|
||||
LogoutPath string // BasePath-relative, e.g. "/logout"; rendered as a POST form (never a GET link); "" hides it
|
||||
NavbarBeforeBrand template.HTML // extra markup rendered before the brand link, e.g. a mobile sidebar toggle
|
||||
NavbarLeftExtra template.HTML // extra markup rendered after Subtitle in the navbar's left group
|
||||
NavbarRightExtra template.HTML // extra markup rendered after Username/Logout in the navbar's right group
|
||||
|
||||
@@ -178,3 +178,25 @@ func TestThemeBlocksInSync(t *testing.T) {
|
||||
t.Errorf("light and prefers-color-scheme: light blocks differ:\nlight: %v\nsystem: %v", light, system)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLogoutIsPostForm pins the navbar's logout control to a POST form:
|
||||
// a GET link would let any foreign page (an <img>, a link prefetcher)
|
||||
// log the user out.
|
||||
func TestLogoutIsPostForm(t *testing.T) {
|
||||
r, err := NewRenderer(testPagesFS(), "pages", []string{"dashboard"}, ".html.tmpl", testFuncs())
|
||||
if err != nil {
|
||||
t.Fatalf("NewRenderer: %v", err)
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
data := PageHeader{Title: "Dashboard", BasePath: "/ui", Username: "alice", LogoutPath: "/logout"}
|
||||
if err := r.Page(&buf, "dashboard", data); err != nil {
|
||||
t.Fatalf("Page: %v", err)
|
||||
}
|
||||
out := buf.String()
|
||||
if !strings.Contains(out, `<form method="post" action="/ui/logout"`) {
|
||||
t.Errorf("logout is not a POST form\n---\n%s", out)
|
||||
}
|
||||
if strings.Contains(out, `href="/ui/logout"`) {
|
||||
t.Errorf("logout still rendered as a GET link\n---\n%s", out)
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@
|
||||
</div>
|
||||
<div class="d-flex align-items-center gap-2">
|
||||
{{if .Username}}<span class="navbar-subtitle">{{.Username}}</span>{{end}}
|
||||
{{if .LogoutPath}}<a href="{{.BasePath}}{{.LogoutPath}}" class="btn btn-outline-secondary btn-sm">Logout</a>{{end}}
|
||||
{{if .LogoutPath}}<form method="post" action="{{.BasePath}}{{.LogoutPath}}" class="d-inline m-0"><button type="submit" class="btn btn-outline-secondary btn-sm">Logout</button></form>{{end}}
|
||||
{{.NavbarRightExtra}}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user