security/routes/manage.go

57 lines
1012 B
Go

package routes
import (
"log"
"git.matterlinux.xyz/matter/security/lib"
"github.com/gofiber/fiber/v2"
)
func MiddleAuth(c *fiber.Ctx) error {
cookie := c.Cookies("auth")
if cookie == "" {
return c.Redirect("/login")
}
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.Cookie == "notset" || u.Cookie == "" {
continue
}
if cookie == u.Cookie {
return c.Next()
}
}
c.ClearCookie("auth")
return c.Redirect("/login")
}
func GETManage(c *fiber.Ctx) error {
return c.Render("manage", fiber.Map{})
}
func GETLogout(c *fiber.Ctx) error {
user, err := lib.GetUser(c)
if err != nil {
log.Printf("Failed to load user: %s", err.Error())
return lib.RenderError(c, 500)
}
user.Cookie = "notset"
err = lib.UpdateUser(user)
if err != nil {
log.Printf("Failed to save users: %s", err.Error())
return lib.RenderError(c, 500)
}
return c.Redirect("/login")
}