security/routes/manage.go

56 lines
1.0 KiB
Go
Raw Permalink Normal View History

2024-03-21 20:20:24 +00:00
package routes
import (
"git.matterlinux.xyz/matter/security/lib"
2024-08-13 19:40:44 +00:00
"git.matterlinux.xyz/matter/security/log"
2024-03-21 20:20:24 +00:00
"github.com/gofiber/fiber/v2"
)
func MiddleAuth(c *fiber.Ctx) error {
2024-08-13 19:25:36 +00:00
cookie := c.Cookies("auth")
if cookie == "" {
return c.Redirect("/login")
}
users, err := lib.LoadUsers()
if err != nil {
2024-08-13 19:40:44 +00:00
log.Error("Failed to load users: %s", err.Error())
2024-08-13 19:25:36 +00:00
return lib.RenderError(c, 500)
}
for _, u := range users {
if u.Cookie == "notset" || u.Cookie == "" {
continue
}
if cookie == u.Cookie {
return c.Next()
}
}
c.ClearCookie("auth")
return c.Redirect("/login")
2024-03-21 20:20:24 +00:00
}
2024-08-13 19:40:44 +00:00
func GET_Manage(c *fiber.Ctx) error {
2024-08-13 19:25:36 +00:00
return c.Render("manage", fiber.Map{})
2024-03-21 20:20:24 +00:00
}
2024-08-13 19:40:44 +00:00
func GET_Logout(c *fiber.Ctx) error {
2024-08-13 19:25:36 +00:00
user, err := lib.GetUser(c)
if err != nil {
2024-08-13 19:40:44 +00:00
log.Error("Failed to load user: %s", err.Error())
2024-08-13 19:25:36 +00:00
return lib.RenderError(c, 500)
}
user.Cookie = "notset"
err = lib.UpdateUser(user)
if err != nil {
2024-08-13 19:40:44 +00:00
log.Error("Failed to save users: %s", err.Error())
2024-08-13 19:25:36 +00:00
return lib.RenderError(c, 500)
}
return c.Redirect("/login")
2024-03-21 20:20:24 +00:00
}