forked from tangled.org/core
this repo has no description

fix 502 on logout

my hunch is that this is caused by using a GET request, which browsers tend to cache sometimes.

Changed files
+16 -4
appview
auth
pages
templates
layouts
state
+7 -1
appview/auth/auth.go
···
}
func (a *Auth) ClearSession(r *http.Request, w http.ResponseWriter) error {
-
clientSession, _ := a.Store.Get(r, appview.SessionName)
clientSession.Options.MaxAge = -1
return clientSession.Save(r, w)
}
···
}
func (a *Auth) ClearSession(r *http.Request, w http.ResponseWriter) error {
+
clientSession, err := a.Store.Get(r, appview.SessionName)
+
if err != nil {
+
return fmt.Errorf("invalid session", err)
+
}
+
if clientSession.IsNew {
+
return fmt.Errorf("invalid session")
+
}
clientSession.Options.MaxAge = -1
return clientSession.Save(r, w)
}
+6 -1
appview/pages/templates/layouts/topbar.html
···
<a href="/{{ didOrHandle .Did .Handle }}">profile</a>
<a href="/knots">knots</a>
<a href="/settings">settings</a>
-
<a href="/logout" class="text-red-400 hover:text-red-700 dark:text-red-400 dark:hover:text-red-300">logout</a>
</div>
</details>
{{ end }}
···
<a href="/{{ didOrHandle .Did .Handle }}">profile</a>
<a href="/knots">knots</a>
<a href="/settings">settings</a>
+
<a href="#"
+
hx-post="/logout"
+
hx-swap="none"
+
class="text-red-400 hover:text-red-700 dark:text-red-400 dark:hover:text-red-300">
+
logout
+
</a>
</div>
</details>
{{ end }}
+1 -1
appview/state/router.go
···
r.Get("/", s.Timeline)
-
r.With(AuthMiddleware(s)).Get("/logout", s.Logout)
r.Route("/login", func(r chi.Router) {
r.Get("/", s.Login)
···
r.Get("/", s.Timeline)
+
r.With(AuthMiddleware(s)).Post("/logout", s.Logout)
r.Route("/login", func(r chi.Router) {
r.Get("/", s.Login)
+2 -1
appview/state/state.go
···
func (s *State) Logout(w http.ResponseWriter, r *http.Request) {
s.auth.ClearSession(r, w)
-
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
}
func (s *State) Timeline(w http.ResponseWriter, r *http.Request) {
···
func (s *State) Logout(w http.ResponseWriter, r *http.Request) {
s.auth.ClearSession(r, w)
+
w.Header().Set("HX-Redirect", "/login")
+
w.WriteHeader(http.StatusSeeOther)
}
func (s *State) Timeline(w http.ResponseWriter, r *http.Request) {