fix: preserve login session across refreshes

This commit is contained in:
OpenClaw Agent
2026-09-22 04:48:06 +08:00
parent f8b5b585c9
commit 7b2c5f444b
3 changed files with 29 additions and 6 deletions
+10
View File
@@ -146,11 +146,20 @@ func clientIPString(r *http.Request) string {
func writeJSON(w http.ResponseWriter, v any) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Vary", "Cookie")
json.NewEncoder(w).Encode(v)
}
func noStore(w http.ResponseWriter) {
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Vary", "Cookie")
}
func writeJSONError(w http.ResponseWriter, status int, message string) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Vary", "Cookie")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(map[string]string{"error": message})
}
@@ -511,6 +520,7 @@ func (s *Server) exist(w http.ResponseWriter, r *http.Request) {
}
func (s *Server) session(w http.ResponseWriter, r *http.Request) {
noStore(w)
if r.Method == http.MethodDelete {
http.SetCookie(w, &http.Cookie{Name: "JWT", Value: "", Path: "/", MaxAge: -1, Expires: time.Unix(1, 0), HttpOnly: true, SameSite: http.SameSiteLaxMode})
http.SetCookie(w, &http.Cookie{Name: "user", Value: "", Path: "/", MaxAge: -1, Expires: time.Unix(1, 0), SameSite: http.SameSiteLaxMode})