From 48a84eb03f6b17ee41d6f097c78d89488ba14e8c Mon Sep 17 00:00:00 2001 From: Alexander NeonXP Kiryukhin Date: Tue, 30 Jul 2024 00:46:05 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=81=D0=B5=D1=81=D1=81=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/cmd/serve.go | 6 ++++-- controllers/user.go | 11 +++++++++-- middleware/user.go | 3 ++- utils/render.go | 15 --------------- 4 files changed, 15 insertions(+), 20 deletions(-) delete mode 100644 utils/render.go 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()) -}