Compare commits

..

No commits in common. "acda30ee332a82324051dc6a9198e8db856c9656" and "bd82bfeca0b9c2a4edacd7cc478f2ca7000628c9" have entirely different histories.

22 changed files with 51 additions and 168 deletions

15
.vscode/launch.json vendored
View File

@ -1,15 +0,0 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "cmd/main.go"
}
]
}

View File

@ -1,13 +1,6 @@
{
"editor.formatOnSave": true,
"[templ]": {
"editor.defaultFormatter": "a-h.templ"
},
"files.exclude": {
"**/*_templ.go": true,
"**/*_templ.txt": true
},
"emmet.includeLanguages": {
"templ": "html"
}
}

View File

@ -1,9 +0,0 @@
package domain
const (
CookieToken = "token"
CookieRefreshToken = "refresh"
CookieUser = "user"
CookieSettingsDarkMode = "darkmode"
)

View File

@ -3,7 +3,6 @@ package handlers
import (
"log"
"net/http"
"templ-test/domain"
"templ-test/views/auth"
"templ-test/views/home"
@ -26,22 +25,22 @@ func (h *Handlers) AuthLoginPost(c echo.Context) error {
}
cookie := new(http.Cookie)
cookie.Name = domain.CookieToken
cookie.Name = CookieToken
cookie.Value = resp.Token
c.SetCookie(cookie)
cookie = new(http.Cookie)
cookie.Name = domain.CookieRefreshToken
cookie.Name = CookieRefreshToken
cookie.Value = resp.RefreshToken
c.SetCookie(cookie)
cookie = new(http.Cookie)
cookie.Name = domain.CookieUser
cookie.Name = CookieUser
cookie.Value = user
c.SetCookie(cookie)
// render
return Render(c, http.StatusOK, auth.LoginPost())
return Render(c, http.StatusOK, home.Home())
}
func (h *Handlers) AuthShowCookies(c echo.Context) error {

View File

@ -1,10 +1,8 @@
package handlers
import (
"context"
"errors"
"templ-test/client"
"templ-test/domain"
"templ-test/models"
"templ-test/services"
"time"
@ -15,6 +13,12 @@ import (
"github.com/labstack/echo/v4"
)
const (
CookieToken = "token"
CookieRefreshToken = "refresh"
CookieUser = "user"
)
type Handlers struct {
api client.ApiClient
cfg services.EnvConfig
@ -44,16 +48,7 @@ func (h *Handlers) Register(group echo.Group) {
func Render(ctx echo.Context, statusCode int, t templ.Component) error {
ctx.Response().Writer.WriteHeader(statusCode)
ctx.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)
// 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)
}
return t.Render(request, ctx.Response().Writer)
return t.Render(ctx.Request().Context(), ctx.Response().Writer)
}
type jwtToken struct {
@ -81,7 +76,7 @@ func ValidateJwt(ctx echo.Context, sharedSecret, issuer string) (jwtToken, error
if !token.Valid {
return jwtToken{}, errors.New("invalid jwt token")
}
claims := token.Claims.(*jwtToken)
if !claims.Exp.After(time.Now()) {
return jwtToken{}, errors.New("the jwt token has expired")
@ -96,25 +91,20 @@ func ValidateJwt(ctx echo.Context, sharedSecret, issuer string) (jwtToken, error
func GetCookieValues(ctx echo.Context) models.AllCookies {
m := models.AllCookies{}
token, err := ctx.Cookie(domain.CookieToken)
token, err := ctx.Cookie(CookieToken)
if err == nil {
m.Token = token.Value
}
user, err := ctx.Cookie(domain.CookieUser)
user, err := ctx.Cookie(CookieUser)
if err == nil {
m.Username = user.Value
}
refresh, err := ctx.Cookie(domain.CookieRefreshToken)
refresh, err := ctx.Cookie(CookieRefreshToken)
if err == nil {
m.RefreshToken = refresh.Value
}
darkMode, err := ctx.Cookie(domain.CookieSettingsDarkMode)
if err == nil {
m.DarkMode = darkMode.Value
}
return m
}

View File

@ -2,8 +2,6 @@ package handlers
import (
"net/http"
"templ-test/domain"
"templ-test/models"
"templ-test/views/home"
"github.com/labstack/echo/v4"
@ -14,25 +12,15 @@ func (h *Handlers) HomeHandler(c echo.Context) error {
}
func (h *Handlers) Settings(c echo.Context) error {
m := models.SettingsViewModel{}
darkMode, err := c.Cookie(domain.CookieSettingsDarkMode)
if err == nil {
m.DarkMode = darkMode.Value
}
return Render(c, http.StatusOK, home.UserSettings(m))
return Render(c, http.StatusOK, home.UserSettings())
}
func (h *Handlers) SettingsPost(c echo.Context) error {
// take in the updated values from he user and write the cookies... tbd
useDarkMode := c.FormValue(domain.CookieSettingsDarkMode)
if useDarkMode != "" {
darkModeCookie := new(http.Cookie)
darkModeCookie.Name = domain.CookieSettingsDarkMode
darkModeCookie.Value = useDarkMode
c.SetCookie(darkModeCookie)
}
return Render(c, http.StatusOK, home.SettingsUpdated())
return Render(c, http.StatusOK, home.UserSettings())
}
//func (h *Handlers) ListHandler(c echo.Context) error {
// return Render(c, http.StatusOK, views.List())
//}

View File

@ -4,7 +4,6 @@ type AllCookies struct {
Username string
Token string
RefreshToken string
DarkMode string
}
type ShowCookie struct {

View File

@ -1,5 +0,0 @@
package models
type SettingsViewModel struct {
DarkMode string
}

View File

@ -4,13 +4,10 @@ import "templ-test/models"
import "templ-test/views/layout"
templ ShowCookie(m models.AllCookies) {
@layout.WithLayout("Cookie Explorer") {
@layout.Testing("Cookie Explorer") {
<h2>These are stored as cookies</h2>
<h3>JWT Values</h3>
<p>Username: { m.Username }</p>
<p>JWT Token: { m.Token }</p>
<p>RefreshToken: { m.RefreshToken }</p>
<h3>User Settings</h3>
<p>DarkMode: { m.DarkMode }</p>
}
}

View File

@ -3,7 +3,7 @@ package auth
import "templ-test/views/layout"
templ AuthLogin() {
@layout.WithLayout("Login") {
@layout.WithLayout("Login", true) {
<form hx-post="/auth/login">
<div class="mb-3">
<label for="username" class="form-label">Username</label>

View File

@ -1,7 +0,0 @@
package auth
import "templ-test/views/components/bootstrap"
templ LoginPost() {
@bootstrap.BootstrapAlert("Login successful!", bootstrap.VariantSuccess)
}

View File

@ -17,3 +17,7 @@ templ BootstrapAlert(message, variant string) {
{ message }
</div>
}
templ BootstrapButton(message, variant string) {
<button type="button" class={ getButtonVariant(variant) }>{ message }</button>
}

View File

@ -1,10 +0,0 @@
package bootstrap
const (
ButtonTypeSubmit = "submit"
ButtonTypeDefault = "button"
)
templ BootstrapButton(message, variant, buttonType string) {
<button type={ buttonType } class={ getButtonVariant(variant) }>{ message }</button>
}

View File

@ -1,5 +0,0 @@
package bootstrap
templ Label(cssClass, name, cssForId, message string) {
<label class={ cssClass } name={ name } for={ cssForId }>{ message }</label>
}

View File

@ -1,13 +0,0 @@
package bootstrap
const (
SwitchTypeCheckbox = "checkbox"
)
templ Switch(name, switchType, cssId string, enabled bool) {
if enabled == true {
<input class="form-check-input" name={ name } type={ switchType } role="switch" id={ cssId } checked/>
} else {
<input class="form-check-input" name={ name } type={ switchType } role="switch" id={ cssId }/>
}
}

View File

@ -3,7 +3,7 @@ package home
import "templ-test/views/layout"
templ Error(message error) {
@layout.WithLayout("Error") {
@layout.Testing("Error") {
<h1>Oops... :(</h1>
<h3>{ message.Error() } </h3>
}

View File

@ -4,14 +4,14 @@ import "templ-test/views/components/bootstrap"
import "templ-test/views/layout"
templ Home() {
@layout.WithLayout("Home") {
@layout.WithLayout("Home", true) {
<p>
this should be above the alert
</p>
@bootstrap.BootstrapAlert("Testing!", bootstrap.VariantDark)
<p>you should now see this under the Alert </p>
@bootstrap.BootstrapButton("I am in danger", bootstrap.VariantDanger, bootstrap.ButtonTypeDefault)
@bootstrap.BootstrapButton("I am the darkness", bootstrap.VariantDark, bootstrap.ButtonTypeDefault)
@bootstrap.BootstrapButton("I am in danger", bootstrap.VariantDanger)
@bootstrap.BootstrapButton("I am the darkness", bootstrap.VariantDark)
}
}

View File

@ -1,33 +1,9 @@
package home
import "templ-test/views/layout"
import "templ-test/views/components/bootstrap"
import "templ-test/models"
import "templ-test/domain"
const (
test = "t"
)
templ UserSettings(model models.SettingsViewModel) {
@layout.WithLayout("Settings") {
<form hx-post="/settings">
<div class="mb-3">
if useDarkMode(model.DarkMode) {
@bootstrap.Switch("darkmode", bootstrap.SwitchTypeCheckbox, test, true)
} else {
@bootstrap.Switch("darkmode", bootstrap.SwitchTypeCheckbox, "flexSwitchCheckDefault", true)
}
@bootstrap.Label("form-check-label", domain.CookieSettingsDarkMode, "flexSwitchCheckDefault", "Use Dark Mode")
</div>
@bootstrap.BootstrapButton("Submit", bootstrap.VariantInfo, bootstrap.ButtonTypeSubmit)
</form>
templ UserSettings() {
@layout.Testing("Settings") {
<h2>This is not ready yet</h2>
}
}
func useDarkMode(value string) bool {
if value == "on" {
return true
}
return false
}

View File

@ -1,5 +0,0 @@
package home
templ SettingsUpdated() {
<p>You are now free to move about the cabin. Thank you for saving with us!</p>
}

View File

@ -1,8 +1,20 @@
package layout
templ WithLayout(pageName string) {
<!doctype html>
<html lang="en" data-bs-theme={ useLightOrDarkTheme(ctx) }>
templ WithLayout(pageName string, useDarkMode bool) {
<html>
@getHtmlHead()
<body>
@bootstrapNavBar()
@getBodyHeader(pageName)
<div class="container-fluid">
{ children... }
</div>
</body>
</html>
}
templ Testing(pageName string) {
<html>
@getHtmlHead()
<body>
@bootstrapNavBar()

View File

@ -1,7 +1,7 @@
package layout
templ bootstrapNavBar() {
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<nav class="navbar navbar-expand-lg bg-body-tertiary" data-bs-theme={ useLightOrDarkTheme(ctx)}>
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">

View File

@ -2,14 +2,8 @@ package layout
import (
"context"
"templ-test/domain"
)
func useLightOrDarkTheme(ctx context.Context) string {
value := ctx.Value(domain.CookieSettingsDarkMode)
if value == "on" {
return "dark"
} else {
return "light"
}
return "dark"
}