From cc621582f9db15e2938fab79fa092d40fd085cb1 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sat, 25 May 2024 16:53:49 +0200 Subject: [PATCH] read session to login --- internal/server/auth.go | 10 ++++++---- internal/server/handle_auth.go | 3 --- internal/server/routes.go | 2 +- internal/view/login.templ | 2 +- internal/view/login_templ.go | 2 +- 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/internal/server/auth.go b/internal/server/auth.go index 4383984..f439adc 100644 --- a/internal/server/auth.go +++ b/internal/server/auth.go @@ -6,6 +6,7 @@ import ( "github.com/labstack/echo-contrib/session" "github.com/labstack/echo/v4" + "github.com/labstack/gommon/log" ) const ( @@ -13,7 +14,7 @@ const ( routeLogin = "/" ) -func MakeAuthMiddleware(secretKey string) echo.MiddlewareFunc { +func MakeAuthMiddleware() echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { if c.Request().RequestURI == routeLogin { @@ -21,8 +22,9 @@ func MakeAuthMiddleware(secretKey string) echo.MiddlewareFunc { } s, err := readSession(c) - if s != "foo" || err != nil { - return c.Redirect(http.StatusUnauthorized, routeLogin) + log.Warnf("%=v", s) + if s != "bar" || err != nil { + return c.Redirect(http.StatusSeeOther, routeLogin) } return next(c) } @@ -34,5 +36,5 @@ func readSession(c echo.Context) (string, error) { if err != nil { return "", err } - return fmt.Sprintf("foo=%v\n", sess.Values["foo"]), nil + return fmt.Sprintf("%s", sess.Values["foo"]), nil } diff --git a/internal/server/handle_auth.go b/internal/server/handle_auth.go index 91b19ed..8a9e8c1 100644 --- a/internal/server/handle_auth.go +++ b/internal/server/handle_auth.go @@ -6,7 +6,6 @@ import ( "github.com/gorilla/sessions" "github.com/labstack/echo-contrib/session" "github.com/labstack/echo/v4" - "github.com/labstack/gommon/log" "github.com/rjNemo/rentease/internal/auth" "github.com/rjNemo/rentease/internal/view" ) @@ -26,7 +25,6 @@ func handleLogin(as *auth.Service) echo.HandlerFunc { return func(c echo.Context) error { sess, err := session.Get(sessionName, c) if err != nil { - log.Warn(err) return err } sess.Options = &sessions.Options{ @@ -41,7 +39,6 @@ func handleLogin(as *auth.Service) echo.HandlerFunc { sess.Values["foo"] = "bar" if err := sess.Save(c.Request(), c.Response()); err != nil { - log.Warn(err) return err } return c.Redirect(http.StatusSeeOther, "/bookings") diff --git a/internal/server/routes.go b/internal/server/routes.go index 1a075ab..2d3f318 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -14,7 +14,7 @@ func (s Server) MountHandlers() { s.Router.POST("/", handleLogin(s.as)) // admin g := s.Router.Group("") - g.Use(MakeAuthMiddleware(s.secretKey)) + g.Use(MakeAuthMiddleware()) g.GET("/bookings", handleListBookingPage(s.bs, s.hc)) g.GET("/bookings/new", handleNewBookingPage(s.hc)) g.POST("/bookings/new", handleCreateBooking(s.bs)) diff --git a/internal/view/login.templ b/internal/view/login.templ index 31449eb..f9d3346 100644 --- a/internal/view/login.templ +++ b/internal/view/login.templ @@ -9,7 +9,7 @@ templ Login() {

Welcome

-
+ diff --git a/internal/view/login_templ.go b/internal/view/login_templ.go index 9f3673a..9bb217a 100644 --- a/internal/view/login_templ.go +++ b/internal/view/login_templ.go @@ -33,7 +33,7 @@ func Login() templ.Component { templ_7745c5c3_Buffer = templ.GetBuffer() defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Welcome

") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Welcome

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err }