aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/cmd/serve.go6
-rw-r--r--controllers/user.go11
-rw-r--r--middleware/user.go3
-rw-r--r--utils/render.go15
4 files changed, 15 insertions, 20 deletions
diff --git a/app/cmd/serve.go b/app/cmd/serve.go
index 578de1e..4222580 100644
--- a/app/cmd/serve.go
+++ b/app/cmd/serve.go
@@ -95,12 +95,14 @@ func serve(ctx context.Context) error {
e.Handle("/assets/", http.StripPrefix("/assets", http.FileServerFS(assets.FS)))
+ sessionManager := session.New(session.NewBoltStore(orm, []byte("sessions")))
+
mh := mux.Use(e,
middleware.Logger(slog.Default()),
- middleware.Recover(slog.Default()),
+ // middleware.Recover(slog.Default()),
middleware.RequestID,
appmiddleware.UserMiddleware(),
- session.Middleware(session.DefaultConfig, session.NewBoltStore(orm, []byte("sessions"))),
+ sessionManager.Middleware(),
)
slog.InfoContext(ctx, "started gorum", slog.String("bind", listen))
diff --git a/controllers/user.go b/controllers/user.go
index a605c25..6685383 100644
--- a/controllers/user.go
+++ b/controllers/user.go
@@ -24,7 +24,13 @@ func (rt *Router) Login(w http.ResponseWriter, r *http.Request) {
}
sess := session.FromRequest(r)
- (*sess)["user"] = u
+ values := sess.Values(r.Context())
+ values["user"] = u
+
+ if err := sess.Save(w, r, values); err != nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(err))
+ return
+ }
mux.Redirect(w, 302, "/")
return
@@ -34,7 +40,8 @@ func (rt *Router) Login(w http.ResponseWriter, r *http.Request) {
}
func (rt *Router) Logout(w http.ResponseWriter, r *http.Request) {
- session.Clear(w, r)
+ sess := session.FromRequest(r)
+ sess.Clear(w, r)
mux.Redirect(w, 302, "/")
}
diff --git a/middleware/user.go b/middleware/user.go
index f4eb12a..419061f 100644
--- a/middleware/user.go
+++ b/middleware/user.go
@@ -13,7 +13,8 @@ func UserMiddleware() mux.Middleware {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := session.FromRequest(r)
- user := (*session)["user"]
+ values := session.Values(r.Context())
+ user := values["user"]
if user == nil {
h.ServeHTTP(w, r)
return
diff --git a/utils/render.go b/utils/render.go
deleted file mode 100644
index 787c453..0000000
--- a/utils/render.go
+++ /dev/null
@@ -1,15 +0,0 @@
-package utils
-
-import (
- "net/http"
-
- "github.com/a-h/templ"
- "github.com/labstack/echo/v4"
-)
-
-func Render(c echo.Context, cmp templ.Component) error {
- c.Response().WriteHeader(http.StatusOK)
- c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8)
-
- return cmp.Render(c.Request().Context(), c.Response())
-}