From f860da87ce8b36b221389fc8cfb3a69a443c0a63 Mon Sep 17 00:00:00 2001 From: Preston Baxter Date: Sun, 29 Oct 2023 18:10:32 -0500 Subject: [PATCH] B: login page tracks whether user is logged in or not --- ui/controllers/auth_middleware.go | 5 ++++- ui/controllers/controllers.go | 2 +- ui/controllers/pages.go | 9 ++++++++- ui/templates/landing_page.templ | 6 ++++-- ui/templates/login_page.templ | 4 ++-- ui/templates/nav.templ | 29 +++++++++++++++++++---------- 6 files changed, 38 insertions(+), 17 deletions(-) diff --git a/ui/controllers/auth_middleware.go b/ui/controllers/auth_middleware.go index 347e675..bed23d4 100644 --- a/ui/controllers/auth_middleware.go +++ b/ui/controllers/auth_middleware.go @@ -9,6 +9,8 @@ import ( "github.com/golang-jwt/jwt/v5" ) +const USER_OBJ_KEY = "userObj" + type AuthClaims struct { Subject string `json:"sub"` Expires int64 `json:"exp"` @@ -110,6 +112,7 @@ func AuthMiddleware(strict bool) gin.HandlerFunc { c.AbortWithError(504, nil) } - c.Set("UserObj", user) + //store user object reference in session. + c.Set(USER_OBJ_KEY, user) } } diff --git a/ui/controllers/controllers.go b/ui/controllers/controllers.go index 429886d..9506be6 100644 --- a/ui/controllers/controllers.go +++ b/ui/controllers/controllers.go @@ -24,7 +24,7 @@ func BuildRouter(r *gin.Engine) { ForceColors: true, }) - r.GET("/", LandingPage) + r.GET("/", AuthMiddleware(false), LandingPage) r.GET("/login", AuthMiddleware(false), LoginPage) r.GET("/signup",AuthMiddleware(false), SignUpPage) diff --git a/ui/controllers/pages.go b/ui/controllers/pages.go index 62f47b8..17979b2 100644 --- a/ui/controllers/pages.go +++ b/ui/controllers/pages.go @@ -1,12 +1,19 @@ package controllers import ( + "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/db/models" "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/templates" "github.com/gin-gonic/gin" ) func LandingPage(c *gin.Context) { - renderTempl(c, templates.LandingPage(false)) + if raw, exists := c.Get(USER_OBJ_KEY); exists { + if user, ok := raw.(*models.User); ok { + renderTempl(c, templates.LandingPage(user)) + return + } + } + renderTempl(c, templates.LandingPage(nil)) } func LoginPage(c *gin.Context) { diff --git a/ui/templates/landing_page.templ b/ui/templates/landing_page.templ index edc8c0c..190fd42 100644 --- a/ui/templates/landing_page.templ +++ b/ui/templates/landing_page.templ @@ -1,5 +1,7 @@ package templates +import "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/db/models" + //Head for scripts and such templ Head() { @@ -29,12 +31,12 @@ templ Head() { } -templ LandingPage(auth bool) { +templ LandingPage(user *models.User) { @Head() - @Nav(auth) + @Nav(user) @LandingContent() @Footer() diff --git a/ui/templates/login_page.templ b/ui/templates/login_page.templ index f3893cb..e509455 100644 --- a/ui/templates/login_page.templ +++ b/ui/templates/login_page.templ @@ -5,7 +5,7 @@ templ LoginPage(errorMsg string) { @Head() - @Nav(false) + @Nav(nil) @LoginContent(false, errorMsg) @Footer() @@ -18,7 +18,7 @@ templ SignupPage(errorMsg string) { @Head() - @Nav(false) + @Nav(nil) @LoginContent(true, errorMsg) @Footer() diff --git a/ui/templates/nav.templ b/ui/templates/nav.templ index 2848e77..5cdd41a 100644 --- a/ui/templates/nav.templ +++ b/ui/templates/nav.templ @@ -1,5 +1,9 @@ package templates +import ( + "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/db/models" +) + templ navTitle(title string) { if auth { - - + + + } else {