package controllers import ( "errors" "fmt" "git.readonly.ch/bouzoure/popvaud-people/helpers" "git.readonly.ch/bouzoure/popvaud-people/models" "github.com/gofiber/fiber/v2" "gorm.io/gorm" ) func LoginForm(c *fiber.Ctx) error { sess, err := helpers.GetSessionStore(c) if err != nil { return err } userid := sess.Get("userid") if userid != nil { return fiber.NewError(fiber.StatusForbidden, "Forbidden") } db, err := helpers.GetDatabase() if err != nil { return err } var loginError string if c.Method() == "POST" { email := c.FormValue("email") password := c.FormValue("password") var user models.User result := db.First(&user, "LOWER(email) = LOWER(?)", email) if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) { return err } if helpers.CheckPasswordHash(password, user.Password) { redirectId := c.Query("redirect") redirectUrl := "/" if len(redirectId) > 0 { redirectKey := fmt.Sprintf("redirect-%s", redirectId) redirectVal := sess.Get(redirectKey) if redirectVal != nil { redirectUrl = redirectVal.(string) } } sess.Set("userid", user.ID) sess.Save() return c.Redirect(redirectUrl) } else { loginError = "Email ou mot de passe incorrect" } } return c.Render("login", fiber.Map{ "PageTitle": "Connexion", "LoginError": loginError, }) }