tfstated/pkg/webui/logout.go
Julien Dessaux 895615ad6e
All checks were successful
main / main (push) Successful in 7m12s
main / publish (push) Has been skipped
main / deploy (push) Has been skipped
chore(webui): rewrite the web session code again while preparing for csrf tokens
#60
2025-04-30 22:31:25 +02:00

33 lines
966 B
Go

package webui
import (
"context"
"fmt"
"html/template"
"net/http"
"git.adyxax.org/adyxax/tfstated/pkg/database"
"git.adyxax.org/adyxax/tfstated/pkg/model"
)
var logoutTemplate = template.Must(template.ParseFS(htmlFS, "html/base.html", "html/logout.html"))
func handleLogoutGET(db *database.DB) http.Handler {
type logoutPage struct {
Page *Page
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value(model.SessionContextKey{}).(*model.Session)
sessionId, session, err := db.MigrateSession(session, nil)
if err != nil {
errorResponse(w, r, http.StatusInternalServerError,
fmt.Errorf("failed to migrate session: %w", err))
return
}
setSessionCookie(w, sessionId)
ctx := context.WithValue(r.Context(), model.SessionContextKey{}, session)
render(w, logoutTemplate, http.StatusOK, logoutPage{
Page: makePage(r.WithContext(ctx), &Page{Title: "Logout", Section: "login"}),
})
})
}