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" "github.com/golang-jwt/jwt/v5"
) )
const USER_OBJ_KEY = "userObj"
type AuthClaims struct { type AuthClaims struct {
Subject string `json:"sub"` Subject string `json:"sub"`
Expires int64 `json:"exp"` Expires int64 `json:"exp"`
@ -110,6 +112,7 @@ func AuthMiddleware(strict bool) gin.HandlerFunc {
c.AbortWithError(504, nil) 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, ForceColors: true,
}) })
r.GET("/", LandingPage) r.GET("/", AuthMiddleware(false), LandingPage)
r.GET("/login", AuthMiddleware(false), LoginPage) r.GET("/login", AuthMiddleware(false), LoginPage)
r.GET("/signup",AuthMiddleware(false), SignUpPage) r.GET("/signup",AuthMiddleware(false), SignUpPage)

View File

@ -1,12 +1,19 @@
package controllers package controllers
import ( import (
"git.preston-baxter.com/Preston_PLB/capstone/frontend-service/db/models"
"git.preston-baxter.com/Preston_PLB/capstone/frontend-service/templates" "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/templates"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func LandingPage(c *gin.Context) { 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) { func LoginPage(c *gin.Context) {

View File

@ -1,5 +1,7 @@
package templates package templates
import "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/db/models"
//Head for scripts and such //Head for scripts and such
templ Head() { templ Head() {
@ -29,12 +31,12 @@ templ Head() {
</head> </head>
} }
templ LandingPage(auth bool) { templ LandingPage(user *models.User) {
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
@Head() @Head()
<body class="text-gray-800 antialiased"> <body class="text-gray-800 antialiased">
@Nav(auth) @Nav(user)
@LandingContent() @LandingContent()
</body> </body>
@Footer() @Footer()

View File

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

View File

@ -1,5 +1,9 @@
package templates package templates
import (
"git.preston-baxter.com/Preston_PLB/capstone/frontend-service/db/models"
)
templ navTitle(title string) { templ navTitle(title string) {
<a <a
class="text-sm font-bold leading-relaxed inline-block mr-4 py-2 whitespace-nowrap uppercase text-white" 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) { templ navActionItem(auth bool) {
<li class="flex items-center"> <li class="flex items-center">
if auth { if auth {
<a href="/dashboard"></a> <a href="/dashboard">
<button <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" 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" type="button"
@ -38,6 +42,7 @@ templ navActionItem(auth bool) {
> >
Dashboard Dashboard
</button> </button>
</a>
} else { } else {
<a href="/login"> <a href="/login">
<button <button
@ -52,7 +57,7 @@ templ navActionItem(auth bool) {
</li> </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 "> <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="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"> <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") @navItem("Pricing", "/pricing")
</ul> </ul>
<ul class="flex flex-col lg:flex-row list-none lg:ml-auto"> <ul class="flex flex-col lg:flex-row list-none lg:ml-auto">
@navActionItem(auth) if user != nil {
@navActionItem(true)
} else {
@navActionItem(false)
}
</ul> </ul>
</div> </div>
</div> </div>