features/bootstrapping #1
5
internal/domain/context.go
Normal file
5
internal/domain/context.go
Normal file
@ -0,0 +1,5 @@
|
||||
package domain
|
||||
|
||||
type contextKey string
|
||||
|
||||
var UserNameContext contextKey = "username"
|
@ -13,7 +13,7 @@ import (
|
||||
func (h *Handler) ArticlesList(c echo.Context) error {
|
||||
_, err := ValidateJwt(c, h.config.JwtSecret, h.config.ServerAddress)
|
||||
if err != nil {
|
||||
return Render(c, http.StatusBadRequest, layout.Error(err))
|
||||
return Render(c, http.StatusOK, layout.Error(err))
|
||||
}
|
||||
|
||||
userToken, err := c.Cookie(domain.CookieToken)
|
||||
|
@ -46,4 +46,4 @@ func (h *Handler) UserAfterSignUp(c echo.Context) error {
|
||||
return Render(c, http.StatusBadRequest, users.AfterLogin(msg, false))
|
||||
}
|
||||
return Render(c, http.StatusOK, users.AfterSignUp("Registration Successful!", true))
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
@ -68,11 +69,14 @@ func Render(ctx echo.Context, statusCode int, t templ.Component) error {
|
||||
|
||||
// take the request context and make it a var
|
||||
request := ctx.Request().Context()
|
||||
|
||||
//Check to see if we the echo context has the cookie we are looking for, if so, create a new context based on what we had and add the value
|
||||
//darkMode, err := ctx.Cookie(domain.CookieSettingsDarkMode)
|
||||
//if err == nil {
|
||||
// request = context.WithValue(request, domain.CookieSettingsDarkMode, darkMode.Value)
|
||||
//}
|
||||
username, err := ctx.Cookie(domain.CookieUser)
|
||||
if err == nil {
|
||||
request = context.WithValue(request, domain.UserNameContext, username.Value)
|
||||
} else {
|
||||
request = context.WithValue(request, domain.UserNameContext, "")
|
||||
}
|
||||
|
||||
return t.Render(request, ctx.Response().Writer)
|
||||
}
|
||||
|
6
internal/models/layout.go
Normal file
6
internal/models/layout.go
Normal file
@ -0,0 +1,6 @@
|
||||
package models
|
||||
|
||||
type LayoutIsLoggedInViewModel struct {
|
||||
IsLoggedIn bool
|
||||
Username string
|
||||
}
|
@ -19,39 +19,24 @@ templ navBar() {
|
||||
<div id="navbarBasicExample" class="navbar-menu">
|
||||
<div class="navbar-start">
|
||||
<a class="navbar-item" href="/articles">Articles</a>
|
||||
<a class="navbar-item">Documentation</a>
|
||||
<!--
|
||||
<div class="navbar-item has-dropdown is-hoverable">
|
||||
<a class="navbar-link">
|
||||
More
|
||||
</a>
|
||||
<div class="navbar-dropdown">
|
||||
<a class="navbar-item">
|
||||
About
|
||||
</a>
|
||||
<a class="navbar-item is-selected">
|
||||
Jobs
|
||||
</a>
|
||||
<a class="navbar-item">
|
||||
Contact
|
||||
</a>
|
||||
<hr class="navbar-divider"/>
|
||||
<a class="navbar-item">
|
||||
Report an issue
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
-->
|
||||
<a class="navbar-item">{ getUsername(ctx) }</a>
|
||||
</div>
|
||||
<div class="navbar-end">
|
||||
<div class="navbar-item">
|
||||
<div class="buttons">
|
||||
<a class="button is-primary" href="/users/signup">
|
||||
<strong>Sign up</strong>
|
||||
</a>
|
||||
<a class="button is-light" href="/users/login">
|
||||
Log in
|
||||
</a>
|
||||
if getUsername(ctx) == "" {
|
||||
<a class="button is-primary" href="/users/signup"><strong>Sign up</strong></a>
|
||||
<a class="button is-light" href="/users/login">Log in</a>
|
||||
} else {
|
||||
<div class="navbar-item has-dropdown is-hoverable">
|
||||
<a class="navbar-link">{ getUsername(ctx) }</a>
|
||||
|
||||
<div class="navbar-dropdown">
|
||||
<a class="navbar-item">Profile</a>
|
||||
<a class="navbar-item">Logout</a>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
14
internal/views/layout/util.go
Normal file
14
internal/views/layout/util.go
Normal file
@ -0,0 +1,14 @@
|
||||
package layout
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.jamestombleson.com/jtom38/newsbot-portal/internal/domain"
|
||||
)
|
||||
|
||||
func getUsername(ctx context.Context) string {
|
||||
if theme, ok := ctx.Value(domain.UserNameContext).(string); ok {
|
||||
return theme
|
||||
}
|
||||
return ""
|
||||
}
|
Loading…
Reference in New Issue
Block a user