From 5f0c7883b1ce264f296413c3804973e770b90238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Bouzour=C3=A8ne?= Date: Fri, 27 Dec 2024 17:45:29 +0100 Subject: [PATCH] Create navbar and add template globals --- main.go | 1 + middlewares/mfa.go | 4 +- middlewares/templates.go | 64 +++++++++++++++++++++ static/main.css | 13 +++++ views/partials/header.html | 114 ++++++++++++++++++++++++++++++++++++- 5 files changed, 192 insertions(+), 4 deletions(-) create mode 100644 middlewares/templates.go diff --git a/main.go b/main.go index 0367d17..0cc4978 100644 --- a/main.go +++ b/main.go @@ -90,6 +90,7 @@ func main() { app.Use(middlewares.WelcomeMiddleware) app.Use(middlewares.MfaEnrollMiddleware) app.Use(middlewares.MfaVerifyMiddleware) + app.Use(middlewares.TemplatesMiddleware) // Controllers app.Get("/", controllers.Homepage) diff --git a/middlewares/mfa.go b/middlewares/mfa.go index b745969..94c00f4 100644 --- a/middlewares/mfa.go +++ b/middlewares/mfa.go @@ -11,7 +11,7 @@ import ( ) func MfaEnrollMiddleware(c *fiber.Ctx) error { - if c.Path() == "/login" || c.Path() == "/welcome" || strings.HasPrefix(c.Path(), "/totp/") { + if c.Path() == "/login" || c.Path() == "/welcome" || strings.HasPrefix(c.Path(), "/totp/") || c.Path() == "/logout" { return c.Next() } @@ -60,7 +60,7 @@ func MfaEnrollMiddleware(c *fiber.Ctx) error { } func MfaVerifyMiddleware(c *fiber.Ctx) error { - if c.Path() == "/login" || c.Path() == "/welcome" || strings.HasPrefix(c.Path(), "/totp/") { + if c.Path() == "/login" || c.Path() == "/welcome" || strings.HasPrefix(c.Path(), "/totp/") || c.Path() == "/logout" { return c.Next() } diff --git a/middlewares/templates.go b/middlewares/templates.go new file mode 100644 index 0000000..01c1805 --- /dev/null +++ b/middlewares/templates.go @@ -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() +} diff --git a/static/main.css b/static/main.css index e4f36a9..85bc7f6 100644 --- a/static/main.css +++ b/static/main.css @@ -20,4 +20,17 @@ img#header-logo { .dashboard-tile .feather { height: 42px; width: auto; +} + +.user-photo { + background-color: purple; + color: #fff; + display: inline-block; + width: 25px; + height: 25px; + border-radius: 50%; + font-weight: bold; + text-align: center; + font-size: 12px; + padding-top: 4px; } \ No newline at end of file diff --git a/views/partials/header.html b/views/partials/header.html index ce19c31..60b2914 100644 --- a/views/partials/header.html +++ b/views/partials/header.html @@ -1,5 +1,5 @@ -