From 5774d421e3ce529080c6a381230080bd47b35d5c Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Sun, 15 Oct 2023 21:10:20 +0300 Subject: [PATCH] fix: swap AuthLogin to return the handler instead --- felu.go | 4 +--- handlers/login.go | 32 +++++++++++++++++--------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/felu.go b/felu.go index 4867e2a..309b50f 100644 --- a/felu.go +++ b/felu.go @@ -73,9 +73,7 @@ func setupFrontendRouter() *gin.Engine { auth := r.Group("/auth") { - auth.POST("/login", func(c *gin.Context) { - handlers.AuthLogin(sessionManager, c) - }) + auth.POST("/login", handlers.AuthLogin(sessionManager)) } // routes for the actual application frontend diff --git a/handlers/login.go b/handlers/login.go index 9e6e1a5..41a81fd 100644 --- a/handlers/login.go +++ b/handlers/login.go @@ -20,22 +20,24 @@ type postLoginDetails struct { Password string `form:"password"` } -func AuthLogin(sm *scs.SessionManager, c *gin.Context) { - data := &postLoginDetails{} - if err := c.Bind(data); err != nil { - log.Printf("[felu] ERROR: Could not bind login details: %v", err) - c.String(http.StatusBadRequest, "Could not bind login details") - return - } +func AuthLogin(sm *scs.SessionManager) gin.HandlerFunc { + return func(c *gin.Context) { + data := &postLoginDetails{} + if err := c.Bind(data); err != nil { + log.Printf("[felu] ERROR: Could not bind login details: %v", err) + c.String(http.StatusBadRequest, "Could not bind login details") + return + } - user, err := db.FetchUserWithCreds(data.Email, data.Password) - if err != nil { - c.String(http.StatusUnauthorized, err.Error()) - return - } + user, err := db.FetchUserWithCreds(data.Email, data.Password) + if err != nil { + c.String(http.StatusUnauthorized, err.Error()) + return + } - sm.Put(c.Request.Context(), "user_id", user.Id) + sm.Put(c.Request.Context(), "user_id", user.Id) - c.Header("HX-Redirect", "/manage") - //c.Redirect(http.StatusFound, "/manage") + c.Header("HX-Redirect", "/manage") + //c.Redirect(http.StatusFound, "/manage") + } } -- 2.44.1