package middlewares import ( "fmt" "time" "git.readonly.ch/bouzoure/pop-camarades/helpers" "git.readonly.ch/bouzoure/pop-camarades/models" "github.com/gofiber/fiber/v2" ) type TemplatesGlobals struct { LoggedIn bool TotpVerified bool UserID uint UserEmail string UserFullname string UserIsAdmin bool TimeStart time.Time ColorMode string } func TemplatesMiddleware(c *fiber.Ctx) error { globals := TemplatesGlobals{} globals.TimeStart = c.Context().Time() sess, err := helpers.GetSessionStore(c) if err != nil { return err } globals.ColorMode = "auto" colorMode := sess.Get("color-mode") if colorMode == "dark" || colorMode == "light" { globals.ColorMode = colorMode.(string) } userid := sess.Get("userid") if userid != nil { switch userid.(type) { case uint: default: return fmt.Errorf("type error, userid should be uint") } globals.LoggedIn = true globals.UserID = userid.(uint) db, err := helpers.GetDatabase() if err != nil { return err } var user models.User result := db.First(&user, "id = ?", globals.UserID) if result.Error == nil { globals.UserEmail = user.Email globals.UserFullname = user.Name globals.UserIsAdmin = user.IsAdmin } } totpVerified := sess.Get("totp-verified") if totpVerified != nil { globals.TotpVerified = true } c.Bind(fiber.Map{ "Globals": globals, }) return c.Next() }