67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package middlewares
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"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
|
|
TimeStart time.Time
|
|
}
|
|
|
|
func TemplatesMiddleware(c *fiber.Ctx) error {
|
|
globals := TemplatesGlobals{}
|
|
globals.TimeStart = c.Context().Time()
|
|
|
|
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()
|
|
}
|