From 1980e33d0f48da098771772630f703e5317f2349 Mon Sep 17 00:00:00 2001 From: Teajey <21069848+Teajey@users.noreply.github.com> Date: Sun, 7 Dec 2025 17:45:37 +0900 Subject: [PATCH] Access http session in protected routes --- http/routing/config.go | 3 ++- http/routing/context.go | 4 ++-- http/routing/foreign_wishlist.go | 2 +- http/routing/groups.go | 6 +++--- http/routing/home.go | 4 ++-- http/routing/users.go | 6 +++--- http/routing/wishlist.go | 2 +- 7 files changed, 14 insertions(+), 13 deletions(-) diff --git a/http/routing/config.go b/http/routing/config.go index e7adaf4..2f18f28 100644 --- a/http/routing/config.go +++ b/http/routing/config.go @@ -3,6 +3,7 @@ package routing import ( lishwist "lishwist/core" "lishwist/http/env" + "lishwist/http/response" "net/http" "github.com/Teajey/rsvp" @@ -13,7 +14,7 @@ type HealthProps struct { Config env.Config } -func Health(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { +func Health(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response { if app.Admin() == nil { return rsvp.Ok() } diff --git a/http/routing/context.go b/http/routing/context.go index d9faf46..06769d5 100644 --- a/http/routing/context.go +++ b/http/routing/context.go @@ -11,7 +11,7 @@ import ( "github.com/Teajey/rsvp" ) -func ExpectAppSession(next func(*lishwist.Session, http.Header, *http.Request) rsvp.Response) response.HandlerFunc { +func ExpectAppSession(next func(*lishwist.Session, *response.Session, http.Header, *http.Request) rsvp.Response) response.HandlerFunc { return func(session *response.Session, h http.Header, r *http.Request) rsvp.Response { sessionKey, ok := session.GetValue("sessionKey").(string) if !ok { @@ -29,6 +29,6 @@ func ExpectAppSession(next func(*lishwist.Session, http.Header, *http.Request) r return response.Error(http.StatusInternalServerError, "Something went wrong.") } - return next(appSession, h, r) + return next(appSession, session, h, r) } } diff --git a/http/routing/foreign_wishlist.go b/http/routing/foreign_wishlist.go index a2c4136..1162cf7 100644 --- a/http/routing/foreign_wishlist.go +++ b/http/routing/foreign_wishlist.go @@ -16,7 +16,7 @@ type foreignWishlistProps struct { Gifts []lishwist.Wish } -func ForeignWishlist(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { +func ForeignWishlist(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response { userReference := r.PathValue("userReference") user := app.User() if user.Reference == userReference { diff --git a/http/routing/groups.go b/http/routing/groups.go index e07f153..d50f291 100644 --- a/http/routing/groups.go +++ b/http/routing/groups.go @@ -37,7 +37,7 @@ func AdminGroup(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Resp return response.Data("group_page.gotmpl", p) } -func Group(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { +func Group(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response { user := app.User() if user.IsAdmin { return AdminGroup(app, h, r) @@ -73,7 +73,7 @@ func PublicGroup(s *response.Session, h http.Header, r *http.Request) rsvp.Respo return response.Data("public_group_page.gotmpl", p) } -func GroupPost(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { +func GroupPost(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response { admin := app.Admin() if admin == nil { return response.NotFound() @@ -138,7 +138,7 @@ func GroupPost(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Respo return response.Data("", group) } -func Groups(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { +func Groups(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response { admin := app.Admin() if admin == nil { return response.NotFound() diff --git a/http/routing/home.go b/http/routing/home.go index 758fa60..4599c8d 100644 --- a/http/routing/home.go +++ b/http/routing/home.go @@ -20,7 +20,7 @@ type HomeProps struct { Groups []lishwist.Group } -func Home(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { +func Home(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response { gifts, err := app.GetWishes() if err != nil { log.Printf("Failed to get gifts: %s\n", err) @@ -41,7 +41,7 @@ func Home(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { return response.Data("home.gotmpl", p) } -func HomePost(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { +func HomePost(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response { err := r.ParseForm() if err != nil { return response.Error(http.StatusBadRequest, "Failed to parse form") diff --git a/http/routing/users.go b/http/routing/users.go index 6578bcb..8ffa28b 100644 --- a/http/routing/users.go +++ b/http/routing/users.go @@ -8,7 +8,7 @@ import ( "github.com/Teajey/rsvp" ) -func Users(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { +func Users(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response { admin := app.Admin() if admin == nil { return response.NotFound() @@ -22,7 +22,7 @@ func Users(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response return response.Data("", users) } -func User(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { +func User(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response { admin := app.Admin() if admin == nil { return response.NotFound() @@ -41,7 +41,7 @@ func User(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { return response.Data("", user) } -func UserPost(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { +func UserPost(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response { admin := app.Admin() if admin == nil { return response.NotFound() diff --git a/http/routing/wishlist.go b/http/routing/wishlist.go index a175af1..a0e5ad5 100644 --- a/http/routing/wishlist.go +++ b/http/routing/wishlist.go @@ -40,7 +40,7 @@ func WishlistDelete(app *lishwist.Session, h http.Header, r *http.Request) rsvp. return rsvp.SeeOther("/", "Wish deleted") } -func ForeignWishlistPost(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { +func ForeignWishlistPost(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response { err := r.ParseForm() if err != nil { return response.Error(http.StatusBadRequest, "Failed to parse form")