Create navbar and add template globals
This commit is contained in:
parent
4be15c2f12
commit
432b01f370
5 changed files with 192 additions and 4 deletions
64
middlewares/templates.go
Normal file
64
middlewares/templates.go
Normal 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()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue