B: login page tracks whether user is logged in or not

This commit is contained in:
Preston Baxter 2023-10-29 18:10:32 -05:00
parent d5f3f5e783
commit f860da87ce
6 changed files with 38 additions and 17 deletions

View File

@ -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)
}
}

View File

@ -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)

View File

@ -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) {

View File

@ -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() {
</head>
}
templ LandingPage(auth bool) {
templ LandingPage(user *models.User) {
<!DOCTYPE html>
<html>
@Head()
<body class="text-gray-800 antialiased">
@Nav(auth)
@Nav(user)
@LandingContent()
</body>
@Footer()

View File

@ -5,7 +5,7 @@ templ LoginPage(errorMsg string) {
<html>
@Head()
<body class="text-gray-800 antialiased">
@Nav(false)
@Nav(nil)
@LoginContent(false, errorMsg)
</body>
@Footer()
@ -18,7 +18,7 @@ templ SignupPage(errorMsg string) {
<html>
@Head()
<body class="text-gray-800 antialiased">
@Nav(false)
@Nav(nil)
@LoginContent(true, errorMsg)
</body>
@Footer()

View File

@ -1,5 +1,9 @@
package templates
import (
"git.preston-baxter.com/Preston_PLB/capstone/frontend-service/db/models"
)
templ navTitle(title string) {
<a
class="text-sm font-bold leading-relaxed inline-block mr-4 py-2 whitespace-nowrap uppercase text-white"
@ -30,7 +34,7 @@ templ navItem(name string, link string) {
templ navActionItem(auth bool) {
<li class="flex items-center">
if auth {
<a href="/dashboard"></a>
<a href="/dashboard">
<button
class="bg-white text-gray-800 active:bg-gray-100 text-xs font-bold uppercase px-4 py-2 rounded shadow hover:shadow-md outline-none focus:outline-none lg:mr-1 lg:mb-0 ml-3 mb-3"
type="button"
@ -38,6 +42,7 @@ templ navActionItem(auth bool) {
>
Dashboard
</button>
</a>
} else {
<a href="/login">
<button
@ -52,7 +57,7 @@ templ navActionItem(auth bool) {
</li>
}
templ Nav(auth bool) {
templ Nav(user *models.User) {
<nav class="top-0 absolute z-50 w-full flex flex-wrap items-center justify-between px-2 py-3 ">
<div class="container px-4 mx-auto flex flex-wrap items-center justify-between">
<div class="w-full relative flex justify-between lg:w-auto lg:static lg:block lg:justify-start">
@ -67,7 +72,11 @@ templ Nav(auth bool) {
@navItem("Pricing", "/pricing")
</ul>
<ul class="flex flex-col lg:flex-row list-none lg:ml-auto">
@navActionItem(auth)
if user != nil {
@navActionItem(true)
} else {
@navActionItem(false)
}
</ul>
</div>
</div>