DEVELOPMENT ENVIRONMENT

~liljamo/felu

5774d421e3ce529080c6a381230080bd47b35d5c — Jonni Liljamo 1 year, 1 month ago d553f6e
fix: swap AuthLogin to return the handler instead
2 files changed, 18 insertions(+), 18 deletions(-)

M felu.go
M handlers/login.go
M felu.go => felu.go +1 -3
@@ 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

M handlers/login.go => handlers/login.go +17 -15
@@ 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")
	}
}