From 4d9759c0930d57449657f12eafeb66dfae3e0294 Mon Sep 17 00:00:00 2001 From: Eden Kirin Date: Tue, 6 Feb 2024 16:21:00 +0100 Subject: [PATCH] Request object --- app/data/users.go | 45 ++ app/handlers/home.go | 12 +- app/handlers/interactive.go | 91 ---- app/handlers/login.go | 11 +- app/handlers/logout.go | 7 +- app/handlers/usersession/usersession.go | 113 ---- app/router/requests/requests.go | 81 +++ app/router/router.go | 8 - app/templates/home.templ | 8 +- app/templates/home_templ.go | 8 +- app/templates/interactive.templ | 229 -------- app/templates/interactive_templ.go | 693 ------------------------ app/templates/layout.templ | 17 +- app/templates/layout_templ.go | 52 +- app/templates/login.templ | 8 +- app/templates/login_templ.go | 14 +- app/templates/types.go | 7 +- 17 files changed, 194 insertions(+), 1210 deletions(-) create mode 100644 app/data/users.go delete mode 100644 app/handlers/interactive.go delete mode 100644 app/handlers/usersession/usersession.go create mode 100644 app/router/requests/requests.go delete mode 100644 app/templates/interactive.templ delete mode 100644 app/templates/interactive_templ.go diff --git a/app/data/users.go b/app/data/users.go new file mode 100644 index 0000000..8241368 --- /dev/null +++ b/app/data/users.go @@ -0,0 +1,45 @@ +package data + +import "fiber-sessions/app/types" + +var dbUsers map[string]types.User = map[string]types.User{ + "pero-uuid": { + Id: "pero-uuid", + Username: "pero", + Password: "pero", + FirstName: "Pero", + LastName: "Perić", + }, + "mirko-uuid": { + Id: "mirko-uuid", + Username: "mirko", + Password: "mirko", + FirstName: "Mirko", + LastName: "Mirković", + }, +} + +type usersStorage struct { +} + +var UsersStorage = usersStorage{} + +func (u usersStorage) GetUserById(id string) *types.User { + user, ok := dbUsers[id] + if ok { + return &user + } + return nil +} + +func (u usersStorage) AuthUser(ident string, password string) *types.User { + for _, user := range dbUsers { + if user.Username == ident { + if user.Password == password { + return &user + } + break + } + } + return nil +} diff --git a/app/handlers/home.go b/app/handlers/home.go index 3c9dcb2..92c9e9f 100644 --- a/app/handlers/home.go +++ b/app/handlers/home.go @@ -1,20 +1,20 @@ package handlers import ( - "fiber-sessions/app/handlers/usersession" + "fiber-sessions/app/data" + "fiber-sessions/app/router/requests" "fiber-sessions/app/templates" "github.com/gofiber/fiber/v2" ) func Home(ctx *fiber.Ctx) error { - requestSession := usersession.NewRequestSession(ctx) + request := requests.NewRequest(ctx, data.UsersStorage) pc := templates.PageContext{ - Title: "Welcome to the demo", - ActivePage: "home", - RequestSession: requestSession, + Title: "Welcome to the demo", + ActivePage: "home", } - Render(ctx, templates.Home(pc)) + Render(ctx, templates.Home(request, pc)) return nil } diff --git a/app/handlers/interactive.go b/app/handlers/interactive.go deleted file mode 100644 index fa38f1d..0000000 --- a/app/handlers/interactive.go +++ /dev/null @@ -1,91 +0,0 @@ -package handlers - -import ( - "fiber-sessions/app/data" - "fiber-sessions/app/templates" - "fiber-sessions/app/types" - "strconv" - "strings" - - "github.com/gofiber/fiber/v2" -) - -var pcInteractive templates.PageContext = templates.PageContext{ - Title: "Welcome to the demo - Interactive", - ActivePage: "interactive", -} - -func Interactive(f *fiber.Ctx) error { - Render(f, templates.Interactive(pcInteractive, data.CatBreeds)) - return nil -} - -func InteractiveSwapContent(f *fiber.Ctx) error { - contentIndexStr := f.Query("content") - contentIndex, err := strconv.Atoi(contentIndexStr) - if err != nil { - contentIndex = 0 - } - Render(f, templates.InteractiveSwapContent(pcInteractive, contentIndex)) - return nil -} - -func FilterCatBreeds(f *fiber.Ctx) error { - breedQuery := strings.ToLower(f.Query("breed")) - countryQuery := f.Query("country") - - var catBreeds []types.CatBreed = []types.CatBreed{} - - for _, cb := range data.CatBreeds { - appendToList := - (len(breedQuery) > 0 && - strings.Contains(strings.ToLower(cb.Breed), breedQuery) || - len(breedQuery) == 0) && - ((len(countryQuery) > 0 && - cb.Country == countryQuery) || - len(countryQuery) == 0) - - if appendToList { - catBreeds = append(catBreeds, cb) - } - } - - Render(f, templates.RenderCatBreedsTable(catBreeds)) - return nil -} - -func ValidateForm(f *fiber.Ctx) error { - content := templates.ValidateFormContent{ - Validated: true, - NumValue: f.FormValue("number-value"), - StrValue: f.FormValue("string-value"), - } - - numValue, err := strconv.Atoi(content.NumValue) - if err != nil { - content.HasNumValueError = true - content.NumValueError = "This is not valid number" - } - if !content.HasNumValueError { - if numValue < 0 { - content.HasNumValueError = true - content.NumValueError = "Value is less than 0" - } - if numValue > 100 { - content.HasNumValueError = true - content.NumValueError = "Value is greater than 100" - } - } - - if len(content.StrValue) < 5 { - content.HasStrValueError = true - content.StrValueError = "String length is less than 5" - } - if len(content.StrValue) > 10 { - content.HasStrValueError = true - content.StrValueError = "String length is more than 10" - } - - Render(f, templates.RenderInteractiveForm(content)) - return nil -} diff --git a/app/handlers/login.go b/app/handlers/login.go index 82482f1..e45af0b 100644 --- a/app/handlers/login.go +++ b/app/handlers/login.go @@ -1,7 +1,8 @@ package handlers import ( - "fiber-sessions/app/handlers/usersession" + "fiber-sessions/app/data" + "fiber-sessions/app/router/requests" "fiber-sessions/app/templates" "github.com/gofiber/fiber/v2" @@ -13,13 +14,13 @@ var pcLogin templates.PageContext = templates.PageContext{ } func Login(ctx *fiber.Ctx) error { - pcLogin.RequestSession = usersession.NewRequestSession(ctx) - Render(ctx, templates.RenderLogin(pcLogin)) + request := requests.NewRequest(ctx, data.UsersStorage) + Render(ctx, templates.RenderLogin(request, pcLogin)) return nil } func LoginSubmit(ctx *fiber.Ctx) error { - requestSession := usersession.NewRequestSession(ctx) + request := requests.NewRequest(ctx, data.UsersStorage) content := templates.LoginFormValidationContent{ Validated: true, @@ -39,7 +40,7 @@ func LoginSubmit(ctx *fiber.Ctx) error { } if !hasError { - err := requestSession.LoginUser(content.Username, content.Password) + err := request.LoginUser(content.Username, content.Password) if err == nil { ctx.Set("HX-Location", "/") return nil diff --git a/app/handlers/logout.go b/app/handlers/logout.go index 3406c3f..498cdc0 100644 --- a/app/handlers/logout.go +++ b/app/handlers/logout.go @@ -1,14 +1,15 @@ package handlers import ( - "fiber-sessions/app/handlers/usersession" + "fiber-sessions/app/data" + "fiber-sessions/app/router/requests" "github.com/gofiber/fiber/v2" ) func Logout(ctx *fiber.Ctx) error { - requestSession := usersession.NewRequestSession(ctx) - requestSession.Logout() + request := requests.NewRequest(ctx, data.UsersStorage) + request.Logout() ctx.Redirect("/") return nil diff --git a/app/handlers/usersession/usersession.go b/app/handlers/usersession/usersession.go deleted file mode 100644 index e7afb6b..0000000 --- a/app/handlers/usersession/usersession.go +++ /dev/null @@ -1,113 +0,0 @@ -package usersession - -import ( - "errors" - "fiber-sessions/app/types" - "fmt" - "time" - - "github.com/gofiber/fiber/v2" - "github.com/gofiber/fiber/v2/middleware/session" - "github.com/gofiber/storage/memory/v2" -) - -var sessionStorage = memory.New() -var sessionStore = session.New(session.Config{ - Storage: sessionStorage, -}) - -const SESSION_EXPIRES = time.Minute * 30 - -var dbUsers map[string]types.User = map[string]types.User{ - "pero-uuid": { - Id: "pero-uuid", - Username: "pero", - Password: "pero", - FirstName: "Pero", - LastName: "Perić", - }, - "mirko-uuid": { - Id: "mirko-uuid", - Username: "mirko", - Password: "mirko", - FirstName: "Mirko", - LastName: "Mirković", - }, -} - -type RequestSession struct { - ctx *fiber.Ctx - session *session.Session - User *types.User -} - -func NewRequestSession(ctx *fiber.Ctx) *RequestSession { - session, err := sessionStore.Get(ctx) - if err != nil { - panic(err) - } - - var user *types.User = nil - - userId := session.Get("userId") - fmt.Printf("userId: %+v\n", userId) - if userId != nil { - user = getUserById(userId.(string)) - } - - return &RequestSession{ - ctx: ctx, - session: session, - User: user, - } -} - -func getUserById(id string) *types.User { - user, ok := dbUsers[id] - if ok { - return &user - } - return nil -} - -func getUser(username string, password string) *types.User { - for _, user := range dbUsers { - if user.Username == username { - if user.Password == password { - return &user - } - break - } - } - return nil -} - -func (r *RequestSession) LoginUser(username string, password string) error { - user := getUser(username, password) - - if user == nil { - return errors.New("invalid username or password") - } - r.User = user - - r.session.Set("userId", r.User.Id) - r.session.SetExpiry(SESSION_EXPIRES) - if err := r.session.Save(); err != nil { - panic(err) - } - - return nil -} - -func (r *RequestSession) UserAuthenticated() bool { - return r.User != nil -} - -func (r *RequestSession) Logout() error { - if !r.UserAuthenticated() { - return errors.New("user not authenticated") - } - r.session.Destroy() - r.User = nil - return nil -} diff --git a/app/router/requests/requests.go b/app/router/requests/requests.go new file mode 100644 index 0000000..6322eff --- /dev/null +++ b/app/router/requests/requests.go @@ -0,0 +1,81 @@ +package requests + +import ( + "errors" + "fiber-sessions/app/types" + "time" + + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/session" + "github.com/gofiber/storage/memory/v2" +) + +var sessionStorage = memory.New() +var sessionStore = session.New(session.Config{ + Storage: sessionStorage, +}) + +const SESSION_EXPIRES = time.Minute * 30 + +type UserGetter interface { + GetUserById(id string) *types.User + AuthUser(username string, password string) *types.User +} + +type Request struct { + ctx *fiber.Ctx + session *session.Session + User *types.User + userGetter UserGetter +} + +func NewRequest(ctx *fiber.Ctx, userGetter UserGetter) *Request { + session, err := sessionStore.Get(ctx) + if err != nil { + panic(err) + } + + var user *types.User = nil + + userId := session.Get("userId") + if userId != nil { + user = userGetter.GetUserById(userId.(string)) + } + + return &Request{ + ctx: ctx, + session: session, + userGetter: userGetter, + User: user, + } +} + +func (r *Request) LoginUser(username string, password string) error { + user := r.userGetter.AuthUser(username, password) + + if user == nil { + return errors.New("invalid username or password") + } + r.User = user + + r.session.Set("userId", r.User.Id) + r.session.SetExpiry(SESSION_EXPIRES) + if err := r.session.Save(); err != nil { + panic(err) + } + + return nil +} + +func (r *Request) UserAuthenticated() bool { + return r.User != nil +} + +func (r *Request) Logout() error { + if !r.UserAuthenticated() { + return errors.New("user not authenticated") + } + r.session.Destroy() + r.User = nil + return nil +} diff --git a/app/router/router.go b/app/router/router.go index d8a7e58..c980e0a 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -58,14 +58,6 @@ func initRouter(app *fiber.App) { app.Post("/login", handlers.LoginSubmit) app.Get("/logout", handlers.Logout) - interactiveGroup := app.Group("/interactive") - { - interactiveGroup.Get("", handlers.Interactive) - interactiveGroup.Get("/swap-content", handlers.InteractiveSwapContent) - interactiveGroup.Get("/filter-cat-breeds", handlers.FilterCatBreeds) - interactiveGroup.Post("/validate-form", handlers.ValidateForm) - } - app.Static("/static", STATIC_PATH) } diff --git a/app/templates/home.templ b/app/templates/home.templ index 8ca3c76..be552f5 100644 --- a/app/templates/home.templ +++ b/app/templates/home.templ @@ -1,7 +1,11 @@ package templates -templ Home(pc PageContext) { - @baseLayout(pc) { +import ( + "fiber-sessions/app/router/requests" +) + +templ Home(request *requests.Request, pc PageContext) { + @baseLayout(request, pc) {

Bacon ipsum dolor amet venison bacon picanha jerky tenderloin. Hamburger sausage meatball chislic salami. Ball tip tenderloin chuck, venison strip steak kielbasa short loin bresaola short ribs cupim drumstick shoulder pork t-bone. Burgdoggen cupim meatball picanha meatloaf, jowl leberkas kevin. T-bone jowl ham hock salami tenderloin.

Beef ribs frankfurter corned beef ground round pork, sausage drumstick. Corned beef turducken beef ribs, tongue burgdoggen capicola kielbasa short loin pork belly tail frankfurter ribeye strip steak meatloaf. Alcatra frankfurter salami cupim strip steak leberkas jerky ham hock biltong swine ball tip prosciutto t-bone andouille. Pork loin hamburger chuck frankfurter ham shankle jowl sausage chislic chicken tenderloin. Capicola tongue beef ribs flank, doner sirloin tri-tip pastrami. Porchetta andouille pork fatback jerky, pancetta pig filet mignon leberkas kielbasa sirloin.

Biltong landjaeger drumstick meatball boudin pork chop turducken jerky hamburger filet mignon flank. Venison turducken pork belly, pork chop ground round buffalo strip steak ribeye alcatra pancetta flank salami chislic. Landjaeger flank andouille short ribs. Shoulder beef ribs chuck, corned beef frankfurter turducken prosciutto.

diff --git a/app/templates/home_templ.go b/app/templates/home_templ.go index 9db1acd..e134bf0 100644 --- a/app/templates/home_templ.go +++ b/app/templates/home_templ.go @@ -10,7 +10,11 @@ import "context" import "io" import "bytes" -func Home(pc PageContext) templ.Component { +import ( + "fiber-sessions/app/router/requests" +) + +func Home(request *requests.Request, pc PageContext) templ.Component { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) if !templ_7745c5c3_IsBuffer { @@ -83,7 +87,7 @@ func Home(pc PageContext) templ.Component { } return templ_7745c5c3_Err }) - templ_7745c5c3_Err = baseLayout(pc).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) + templ_7745c5c3_Err = baseLayout(request, pc).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/app/templates/interactive.templ b/app/templates/interactive.templ deleted file mode 100644 index b8a5127..0000000 --- a/app/templates/interactive.templ +++ /dev/null @@ -1,229 +0,0 @@ -package templates - -import ( - "fiber-sessions/app/types" - "fiber-sessions/app/data" - "slices" - "sort" -) - -templ interactiveButtonsSection() { -

- Interactive buttons -

- - - -

- Bacon ipsum dolor amet cow capicola pancetta picanha biltong brisket filet mignon turducken beef ribs burgdoggen landjaeger meatball venison shank. Capicola ham pork chop, biltong kielbasa pancetta short loin jowl cupim pig jerky drumstick turducken burgdoggen beef. Spare ribs flank ribeye cow doner, shank chuck bacon ham hock porchetta kielbasa tri-tip. Ham t-bone chislic, capicola andouille ham hock frankfurter tri-tip sausage kevin landjaeger shank ribeye. Swine tri-tip spare ribs, rump flank bresaola kevin tail. Meatball tail picanha cow, frankfurter ribeye sirloin pork belly short loin pig. Filet mignon spare ribs pastrami, tri-tip ball tip tongue fatback pork chop. -

-} - -func getCatCountriesOptions() []string { - var res []string = []string{} - - for _, cb := range data.CatBreeds { - if len(cb.Country) > 0 && !slices.Contains(res, cb.Country) { - res = append(res, cb.Country) - } - } - - sort.Strings(res) - - return res -} - -templ catBreedsSection(catBreeds []types.CatBreed) { -

- Cat breeds -

-
-
-
-
- -
-
- -
-
- -
-
- -
-
-
-
- @catBreedsTable(catBreeds) -} - -templ catBreedsTable(catBreeds []types.CatBreed) { - - - - - - - - - - - - for _, breed := range catBreeds { - - - - - - - - } - -
BreedCountryOriginCoatPattern
{ breed.Breed }{ breed.Country }{ breed.Origin }{ breed.Coat }{ breed.Pattern }
-} - -templ InteractiveSwapContent(pc PageContext, contentIndex int) { - switch contentIndex { - case 1: -

- Bacon ipsum dolor amet t-bone meatball corned beef hamburger cupim rump drumstick tri-tip doner pastrami kielbasa salami pig. Pork loin doner leberkas, jerky fatback chuck corned beef cow rump strip steak shoulder pork belly. Flank hamburger porchetta shank sirloin bresaola chislic pork chop. Chicken jerky strip steak tongue jowl brisket cupim. Shankle t-bone short ribs fatback burgdoggen buffalo cupim prosciutto meatball pig doner boudin. -

- case 2: -

- Rump venison tenderloin, picanha ribeye cupim pork belly sirloin landjaeger jerky prosciutto filet mignon alcatra. Short ribs pork chop rump kevin t-bone ground round hamburger, chislic tenderloin bacon sirloin filet mignon turducken beef pork loin. Buffalo picanha ground round pork chop. Venison cupim pork belly, andouille pastrami tail shankle pancetta. Picanha leberkas cupim turkey buffalo filet mignon pork belly pork chop rump sausage pork loin. Short ribs burgdoggen porchetta hamburger, cupim ribeye capicola shoulder. Pork drumstick kevin, turkey hamburger ground round burgdoggen chuck jerky bacon sausage chislic leberkas. -

- case 3: -

- Biltong ham hock tongue, beef pork andouille fatback flank pork loin ribeye. Rump short loin porchetta, ground round buffalo hamburger salami pig venison beef ribs. Chislic beef ribs sausage fatback pork belly. Ribeye porchetta leberkas, tail hamburger biltong landjaeger short loin filet mignon chicken salami flank. -

- default: -

- Some new content with unknown index... -

- } -} - -templ RenderCatBreedsTable(catBreeds []types.CatBreed) { - @catBreedsTable(catBreeds) -} - -templ RenderInteractiveForm(content ValidateFormContent) { -
-
-
- - - if content.HasNumValueError { -
{ content.NumValueError }
- } -
-
-
-
- - - if content.HasStrValueError { -
{ content.StrValueError }
- } -
-
-
-} - -templ interactiveFormSection() { -

- Form validation -

-
-
- @RenderInteractiveForm(ValidateFormContent{}) -
- -
-
-
-} - -templ Interactive(pc PageContext, catBreeds []types.CatBreed) { - @baseLayout(pc) { -
-
- @interactiveButtonsSection() -
-
- @interactiveFormSection() -
-
- @catBreedsSection(catBreeds) - } -} diff --git a/app/templates/interactive_templ.go b/app/templates/interactive_templ.go deleted file mode 100644 index 25f8297..0000000 --- a/app/templates/interactive_templ.go +++ /dev/null @@ -1,693 +0,0 @@ -// Code generated by templ - DO NOT EDIT. - -// templ: version: v0.2.513 -package templates - -//lint:file-ignore SA4006 This context is only used if a nested component is present. - -import "github.com/a-h/templ" -import "context" -import "io" -import "bytes" - -import ( - "fiber-sessions/app/data" - "fiber-sessions/app/types" - "slices" - "sort" -) - -func interactiveButtonsSection() templ.Component { - return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) - if !templ_7745c5c3_IsBuffer { - templ_7745c5c3_Buffer = templ.GetBuffer() - defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) - } - ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var1 := templ.GetChildren(ctx) - if templ_7745c5c3_Var1 == nil { - templ_7745c5c3_Var1 = templ.NopComponent - } - ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Var2 := `Interactive buttons` - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var2) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Var6 := `Bacon ipsum dolor amet cow capicola pancetta picanha biltong brisket filet mignon turducken beef ribs burgdoggen landjaeger meatball venison shank. Capicola ham pork chop, biltong kielbasa pancetta short loin jowl cupim pig jerky drumstick turducken burgdoggen beef. Spare ribs flank ribeye cow doner, shank chuck bacon ham hock porchetta kielbasa tri-tip. Ham t-bone chislic, capicola andouille ham hock frankfurter tri-tip sausage kevin landjaeger shank ribeye. Swine tri-tip spare ribs, rump flank bresaola kevin tail. Meatball tail picanha cow, frankfurter ribeye sirloin pork belly short loin pig. Filet mignon spare ribs pastrami, tri-tip ball tip tongue fatback pork chop.` - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var6) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } - return templ_7745c5c3_Err - }) -} - -func getCatCountriesOptions() []string { - var res []string = []string{} - - for _, cb := range data.CatBreeds { - if len(cb.Country) > 0 && !slices.Contains(res, cb.Country) { - res = append(res, cb.Country) - } - } - - sort.Strings(res) - - return res -} - -func catBreedsSection(catBreeds []types.CatBreed) templ.Component { - return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) - if !templ_7745c5c3_IsBuffer { - templ_7745c5c3_Buffer = templ.GetBuffer() - defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) - } - ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var7 := templ.GetChildren(ctx) - if templ_7745c5c3_Var7 == nil { - templ_7745c5c3_Var7 = templ.NopComponent - } - ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Var8 := `Cat breeds` - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var8) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = catBreedsTable(catBreeds).Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } - return templ_7745c5c3_Err - }) -} - -func catBreedsTable(catBreeds []types.CatBreed) templ.Component { - return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) - if !templ_7745c5c3_IsBuffer { - templ_7745c5c3_Buffer = templ.GetBuffer() - defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) - } - ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var13 := templ.GetChildren(ctx) - if templ_7745c5c3_Var13 == nil { - templ_7745c5c3_Var13 = templ.NopComponent - } - ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - for _, breed := range catBreeds { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Var14 := `Breed` - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var14) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Var15 := `Country` - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var15) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Var16 := `Origin` - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var16) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Var17 := `Coat` - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var17) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Var18 := `Pattern` - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var18) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var19 string - templ_7745c5c3_Var19, templ_7745c5c3_Err = templ.JoinStringErrs(breed.Breed) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/templates/interactive.templ`, Line: 121, Col: 22} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var19)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var20 string - templ_7745c5c3_Var20, templ_7745c5c3_Err = templ.JoinStringErrs(breed.Country) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/templates/interactive.templ`, Line: 122, Col: 24} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var20)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var21 string - templ_7745c5c3_Var21, templ_7745c5c3_Err = templ.JoinStringErrs(breed.Origin) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/templates/interactive.templ`, Line: 123, Col: 23} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var21)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var22 string - templ_7745c5c3_Var22, templ_7745c5c3_Err = templ.JoinStringErrs(breed.Coat) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/templates/interactive.templ`, Line: 124, Col: 21} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var22)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var23 string - templ_7745c5c3_Var23, templ_7745c5c3_Err = templ.JoinStringErrs(breed.Pattern) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/templates/interactive.templ`, Line: 125, Col: 24} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var23)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } - return templ_7745c5c3_Err - }) -} - -func InteractiveSwapContent(pc PageContext, contentIndex int) templ.Component { - return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) - if !templ_7745c5c3_IsBuffer { - templ_7745c5c3_Buffer = templ.GetBuffer() - defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) - } - ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var24 := templ.GetChildren(ctx) - if templ_7745c5c3_Var24 == nil { - templ_7745c5c3_Var24 = templ.NopComponent - } - ctx = templ.ClearChildren(ctx) - switch contentIndex { - case 1: - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Var25 := `Bacon ipsum dolor amet t-bone meatball corned beef hamburger cupim rump drumstick tri-tip doner pastrami kielbasa salami pig. Pork loin doner leberkas, jerky fatback chuck corned beef cow rump strip steak shoulder pork belly. Flank hamburger porchetta shank sirloin bresaola chislic pork chop. Chicken jerky strip steak tongue jowl brisket cupim. Shankle t-bone short ribs fatback burgdoggen buffalo cupim prosciutto meatball pig doner boudin.` - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var25) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - case 2: - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Var26 := `Rump venison tenderloin, picanha ribeye cupim pork belly sirloin landjaeger jerky prosciutto filet mignon alcatra. Short ribs pork chop rump kevin t-bone ground round hamburger, chislic tenderloin bacon sirloin filet mignon turducken beef pork loin. Buffalo picanha ground round pork chop. Venison cupim pork belly, andouille pastrami tail shankle pancetta. Picanha leberkas cupim turkey buffalo filet mignon pork belly pork chop rump sausage pork loin. Short ribs burgdoggen porchetta hamburger, cupim ribeye capicola shoulder. Pork drumstick kevin, turkey hamburger ground round burgdoggen chuck jerky bacon sausage chislic leberkas.` - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var26) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - case 3: - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Var27 := `Biltong ham hock tongue, beef pork andouille fatback flank pork loin ribeye. Rump short loin porchetta, ground round buffalo hamburger salami pig venison beef ribs. Chislic beef ribs sausage fatback pork belly. Ribeye porchetta leberkas, tail hamburger biltong landjaeger short loin filet mignon chicken salami flank.` - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var27) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - default: - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Var28 := `Some new content with unknown index...` - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var28) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } - return templ_7745c5c3_Err - }) -} - -func RenderCatBreedsTable(catBreeds []types.CatBreed) templ.Component { - return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) - if !templ_7745c5c3_IsBuffer { - templ_7745c5c3_Buffer = templ.GetBuffer() - defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) - } - ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var29 := templ.GetChildren(ctx) - if templ_7745c5c3_Var29 == nil { - templ_7745c5c3_Var29 = templ.NopComponent - } - ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Err = catBreedsTable(catBreeds).Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } - return templ_7745c5c3_Err - }) -} - -func RenderInteractiveForm(content ValidateFormContent) templ.Component { - return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) - if !templ_7745c5c3_IsBuffer { - templ_7745c5c3_Buffer = templ.GetBuffer() - defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) - } - ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var30 := templ.GetChildren(ctx) - if templ_7745c5c3_Var30 == nil { - templ_7745c5c3_Var30 = templ.NopComponent - } - ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var32 = []any{"form-control", - templ.KV("is-invalid", content.HasNumValueError), - templ.KV("is-valid", content.Validated && !content.HasNumValueError)} - templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var32...) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" ") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - if content.HasNumValueError { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var33 string - templ_7745c5c3_Var33, templ_7745c5c3_Err = templ.JoinStringErrs(content.NumValueError) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/templates/interactive.templ`, Line: 172, Col: 58} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var33)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var35 = []any{"form-control", - templ.KV("is-invalid", content.HasStrValueError), - templ.KV("is-valid", content.Validated && !content.HasStrValueError)} - templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var35...) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" ") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - if content.HasStrValueError { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var36 string - templ_7745c5c3_Var36, templ_7745c5c3_Err = templ.JoinStringErrs(content.StrValueError) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/templates/interactive.templ`, Line: 189, Col: 58} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var36)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } - return templ_7745c5c3_Err - }) -} - -func interactiveFormSection() templ.Component { - return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) - if !templ_7745c5c3_IsBuffer { - templ_7745c5c3_Buffer = templ.GetBuffer() - defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) - } - ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var37 := templ.GetChildren(ctx) - if templ_7745c5c3_Var37 == nil { - templ_7745c5c3_Var37 = templ.NopComponent - } - ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Var38 := `Form validation` - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var38) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = RenderInteractiveForm(ValidateFormContent{}).Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } - return templ_7745c5c3_Err - }) -} - -func Interactive(pc PageContext, catBreeds []types.CatBreed) templ.Component { - return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) - if !templ_7745c5c3_IsBuffer { - templ_7745c5c3_Buffer = templ.GetBuffer() - defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) - } - ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var40 := templ.GetChildren(ctx) - if templ_7745c5c3_Var40 == nil { - templ_7745c5c3_Var40 = templ.NopComponent - } - ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Var41 := templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) - if !templ_7745c5c3_IsBuffer { - templ_7745c5c3_Buffer = templ.GetBuffer() - defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = interactiveButtonsSection().Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = interactiveFormSection().Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = catBreedsSection(catBreeds).Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = io.Copy(templ_7745c5c3_W, templ_7745c5c3_Buffer) - } - return templ_7745c5c3_Err - }) - templ_7745c5c3_Err = baseLayout(pc).Render(templ.WithChildren(ctx, templ_7745c5c3_Var41), templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } - return templ_7745c5c3_Err - }) -} diff --git a/app/templates/layout.templ b/app/templates/layout.templ index ebf422a..495735d 100644 --- a/app/templates/layout.templ +++ b/app/templates/layout.templ @@ -1,6 +1,10 @@ package templates -templ baseLayout(pc PageContext) { +import ( + "fiber-sessions/app/router/requests" +) + +templ baseLayout(request *requests.Request, pc PageContext) { @@ -22,7 +26,7 @@ templ baseLayout(pc PageContext) { Home - if pc.RequestSession.UserAuthenticated() { + if request.UserAuthenticated() { } - - if pc.RequestSession.UserAuthenticated() { + if request.UserAuthenticated() {

- Hello, { pc.RequestSession.User.FirstName } + Hello, { request.User.FirstName }

} { children... } diff --git a/app/templates/layout_templ.go b/app/templates/layout_templ.go index 0885796..b698ec1 100644 --- a/app/templates/layout_templ.go +++ b/app/templates/layout_templ.go @@ -10,7 +10,11 @@ import "context" import "io" import "bytes" -func baseLayout(pc PageContext) templ.Component { +import ( + "fiber-sessions/app/router/requests" +) + +func baseLayout(request *requests.Request, pc PageContext) templ.Component { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) if !templ_7745c5c3_IsBuffer { @@ -39,7 +43,7 @@ func baseLayout(pc PageContext) templ.Component { var templ_7745c5c3_Var3 string templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(pc.Title) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/templates/layout.templ`, Line: 11, Col: 20} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/templates/layout.templ`, Line: 15, Col: 20} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) if templ_7745c5c3_Err != nil { @@ -84,7 +88,7 @@ func baseLayout(pc PageContext) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if pc.RequestSession.UserAuthenticated() { + if request.UserAuthenticated() { _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
  • ") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err @@ -130,52 +134,26 @@ func baseLayout(pc PageContext) templ.Component { return templ_7745c5c3_Err } } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
  • ") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var10 = []any{"nav-link", templ.KV("active", pc.ActivePage == "interactive")} - templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var10...) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Var11 := `Interactive` - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var11) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
  • ") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - if pc.RequestSession.UserAuthenticated() { + if request.UserAuthenticated() { _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

    ") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Var12 := `Hello, ` - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var12) + templ_7745c5c3_Var10 := `Hello, ` + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var10) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var13 string - templ_7745c5c3_Var13, templ_7745c5c3_Err = templ.JoinStringErrs(pc.RequestSession.User.FirstName) + var templ_7745c5c3_Var11 string + templ_7745c5c3_Var11, templ_7745c5c3_Err = templ.JoinStringErrs(request.User.FirstName) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/templates/layout.templ`, Line: 45, Col: 47} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/templates/layout.templ`, Line: 44, Col: 37} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var13)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var11)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/app/templates/login.templ b/app/templates/login.templ index 00a74bc..7227475 100644 --- a/app/templates/login.templ +++ b/app/templates/login.templ @@ -1,5 +1,9 @@ package templates +import ( + "fiber-sessions/app/router/requests" +) + templ LoginFormContent(content LoginFormValidationContent) {

    @@ -49,8 +53,8 @@ templ LoginFormContent(content LoginFormValidationContent) {
    } -templ RenderLogin(pc PageContext) { - @baseLayout(pc) { +templ RenderLogin(request *requests.Request, pc PageContext) { + @baseLayout(request, pc) {

    diff --git a/app/templates/login_templ.go b/app/templates/login_templ.go index 01879bc..155ca1b 100644 --- a/app/templates/login_templ.go +++ b/app/templates/login_templ.go @@ -10,6 +10,10 @@ import "context" import "io" import "bytes" +import ( + "fiber-sessions/app/router/requests" +) + func LoginFormContent(content LoginFormValidationContent) templ.Component { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) @@ -71,7 +75,7 @@ func LoginFormContent(content LoginFormValidationContent) templ.Component { var templ_7745c5c3_Var4 string templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(content.UsernameError) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/templates/login.templ`, Line: 18, Col: 57} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/templates/login.templ`, Line: 22, Col: 57} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) if templ_7745c5c3_Err != nil { @@ -130,7 +134,7 @@ func LoginFormContent(content LoginFormValidationContent) templ.Component { var templ_7745c5c3_Var7 string templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(content.PasswordError) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/templates/login.templ`, Line: 35, Col: 57} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/templates/login.templ`, Line: 39, Col: 57} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7)) if templ_7745c5c3_Err != nil { @@ -153,7 +157,7 @@ func LoginFormContent(content LoginFormValidationContent) templ.Component { var templ_7745c5c3_Var8 string templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(content.LoginError) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/templates/login.templ`, Line: 40, Col: 24} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/templates/login.templ`, Line: 44, Col: 24} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8)) if templ_7745c5c3_Err != nil { @@ -184,7 +188,7 @@ func LoginFormContent(content LoginFormValidationContent) templ.Component { }) } -func RenderLogin(pc PageContext) templ.Component { +func RenderLogin(request *requests.Request, pc PageContext) templ.Component { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) if !templ_7745c5c3_IsBuffer { @@ -229,7 +233,7 @@ func RenderLogin(pc PageContext) templ.Component { } return templ_7745c5c3_Err }) - templ_7745c5c3_Err = baseLayout(pc).Render(templ.WithChildren(ctx, templ_7745c5c3_Var11), templ_7745c5c3_Buffer) + templ_7745c5c3_Err = baseLayout(request, pc).Render(templ.WithChildren(ctx, templ_7745c5c3_Var11), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/app/templates/types.go b/app/templates/types.go index d6a3964..a3d7f6b 100644 --- a/app/templates/types.go +++ b/app/templates/types.go @@ -1,11 +1,8 @@ package templates -import "fiber-sessions/app/handlers/usersession" - type PageContext struct { - Title string - ActivePage string - RequestSession *usersession.RequestSession + Title string + ActivePage string } type ValidateFormContent struct {