package routes import ( "log" "git.matterlinux.xyz/matter/security/lib" "github.com/gofiber/fiber/v2" ) func POSTLogin(c *fiber.Ctx) error { body := struct { Username string `form:"username"` Password string `form:"password"` }{} err := c.BodyParser(&body) if err != nil { return lib.RenderError(c, 400) } users, err := lib.LoadUsers() if err != nil { log.Printf("Failed to load users: %s", err.Error()) return lib.RenderError(c, 500) } for _, u := range users { if u.Username == u.Username && u.Password == lib.GetSHA256([]byte(body.Password)) { u.Cookie = lib.GetRandom() c.Cookie(&fiber.Cookie{ Name: "auth", Value: u.Cookie, }) err = lib.UpdateUser(u) if err != nil { log.Printf("Failed to update user: %s", err.Error()) return lib.RenderError(c, 500) } return c.Redirect("/manage") } } c.Status(403) return c.Render("login", fiber.Map{}) } func GETLogin(c *fiber.Ctx) error { if c.Cookies("auth") != "" { return c.Redirect("/manage") } return c.Render("login", fiber.Map{}) }