Create navbar and add template globals

This commit is contained in:
William Bouzourène 2024-12-27 17:45:29 +01:00
parent 4be15c2f12
commit 432b01f370
Signed by: bouzoure
SSH key fingerprint: SHA256:19MbXpLua4rUtk8tunMesD8KUKb91LXLHg8E/qTooww
5 changed files with 192 additions and 4 deletions

64
middlewares/templates.go Normal file
View file

@ -0,0 +1,64 @@
package middlewares
import (
"fmt"
"git.readonly.ch/bouzoure/popvaud-people/helpers"
"git.readonly.ch/bouzoure/popvaud-people/models"
"github.com/gofiber/fiber/v2"
)
type TemplatesGlobals struct {
LoggedIn bool
TotpVerified bool
UserID uint
UserEmail string
UserFullname string
UserIsAdmin bool
}
func TemplatesMiddleware(c *fiber.Ctx) error {
globals := TemplatesGlobals{}
sess, err := helpers.GetSessionStore(c)
if err != nil {
return err
}
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()
}