From c171b417d8e8f79029508e21f18bfa2e5278d591 Mon Sep 17 00:00:00 2001 From: Atridad Lahiji Date: Tue, 9 Jul 2024 11:10:32 -0600 Subject: [PATCH] Fixed auth flow --- lib/templates.go | 21 +++++++-- lib/user.go | 12 +++++ main.go | 5 ++- pages/dashboard.go | 10 ++--- pages/example.go | 5 ++- pages/home.go | 9 +--- pages/register.go | 4 +- pages/room.go | 8 ++-- pages/signin.go | 4 +- pages/templates/dashboard.html | 2 +- pages/templates/layouts/base.html | 2 - pages/templates/partials/buttonlinks.html | 15 ------- pages/templates/partials/cardlinks.html | 54 ----------------------- pages/templates/partials/iconlinks.html | 5 --- 14 files changed, 49 insertions(+), 107 deletions(-) delete mode 100644 pages/templates/partials/buttonlinks.html delete mode 100644 pages/templates/partials/cardlinks.html delete mode 100644 pages/templates/partials/iconlinks.html diff --git a/lib/templates.go b/lib/templates.go index cd10397..3e839c0 100644 --- a/lib/templates.go +++ b/lib/templates.go @@ -2,14 +2,20 @@ package lib import ( "html/template" - "net/http" "path/filepath" "runtime" templatefs "pollo/pages/templates" + + "github.com/labstack/echo/v4" ) -func RenderTemplate(w http.ResponseWriter, layout string, partials []string, props interface{}) error { +type TemplateData struct { + Props interface{} + IsLoggedIn bool +} + +func RenderTemplate(c echo.Context, layout string, partials []string, props interface{}) error { // Get the name of the current file _, filename, _, _ := runtime.Caller(1) page := filepath.Base(filename) @@ -31,8 +37,15 @@ func RenderTemplate(w http.ResponseWriter, layout string, partials []string, pro return err } - // Execute the layout template - err = ts.ExecuteTemplate(w, layout, props) + // Wrap the props with the IsLoggedIn status + isLoggedIn := IsSignedIn(c) + templateData := TemplateData{ + Props: props, + IsLoggedIn: isLoggedIn, + } + + // Execute the layout template with the wrapped props + err = ts.ExecuteTemplate(c.Response().Writer, layout, templateData) if err != nil { LogError.Print(err.Error()) return err diff --git a/lib/user.go b/lib/user.go index 4a2e478..6fa49a5 100644 --- a/lib/user.go +++ b/lib/user.go @@ -90,6 +90,18 @@ func AuthenticatedPageMiddleware(next echo.HandlerFunc) echo.HandlerFunc { } } +func AuthFlowPageMiddleware(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + isSignedIn := IsSignedIn(c) + + if isSignedIn { + return c.Redirect(http.StatusFound, "/dashboard") + } + + return next(c) + } +} + func AuthenticatedEndpointMiddleware(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { isSignedIn := IsSignedIn(c) diff --git a/main.go b/main.go index a545c2d..9084921 100755 --- a/main.go +++ b/main.go @@ -55,6 +55,7 @@ func main() { e := echo.New() publicPageRoute := e.Group("") protectedPageRoute := e.Group("", lib.AuthenticatedPageMiddleware) + authFlowPageRoute := e.Group("", lib.AuthFlowPageMiddleware) publicApiRoute := e.Group("/api") protectedApiRoute := e.Group("/api", lib.AuthenticatedEndpointMiddleware) webhookGroup := e.Group("/webhook") @@ -85,8 +86,8 @@ func main() { // Page Routes: // ------------------------------ publicPageRoute.GET("/", pages.Home) - publicPageRoute.GET("/signin", pages.SignIn) - publicPageRoute.GET("/register", pages.Register) + authFlowPageRoute.GET("/signin", pages.SignIn) + authFlowPageRoute.GET("/register", pages.Register) protectedPageRoute.GET("/dashboard", pages.Dashboard) protectedPageRoute.GET("/room/:id", pages.Room) diff --git a/pages/dashboard.go b/pages/dashboard.go index ab57c22..e783071 100644 --- a/pages/dashboard.go +++ b/pages/dashboard.go @@ -7,24 +7,22 @@ import ( ) type DashboardProps struct { - IsLoggedIn bool - Name string + Name string } func Dashboard(c echo.Context) error { currentSession, error := lib.GetSessionCookie(c.Request(), "session") if error != nil { - return c.Redirect(302, "/signin") + lib.LogError.Printf("Error getting session: %v", error) } props := DashboardProps{ - IsLoggedIn: lib.IsSignedIn(c), - Name: currentSession.Name, + Name: currentSession.Name, } // Specify the partials used by this page partials := []string{"header"} // Render the template - return lib.RenderTemplate(c.Response().Writer, "base", partials, props) + return lib.RenderTemplate(c, "base", partials, props) } diff --git a/pages/example.go b/pages/example.go index 35cab5f..56b1e36 100644 --- a/pages/example.go +++ b/pages/example.go @@ -1,8 +1,9 @@ package pages import ( - "github.com/labstack/echo/v4" "pollo/lib" + + "github.com/labstack/echo/v4" ) type ExampleProps struct { @@ -18,5 +19,5 @@ func Example(c echo.Context) error { partials := []string{"header", "navitems"} // Render the template - return lib.RenderTemplate(c.Response().Writer, "base", partials, props) + return lib.RenderTemplate(c, "base", partials, props) } diff --git a/pages/home.go b/pages/home.go index 7a5afcc..640da1e 100644 --- a/pages/home.go +++ b/pages/home.go @@ -7,19 +7,14 @@ import ( ) type HomeProps struct { - IsLoggedIn bool } func Home(c echo.Context) error { - props := HomeProps{ - IsLoggedIn: lib.IsSignedIn(c), - } - - println("Home page props: ", props.IsLoggedIn) + props := HomeProps{} // Specify the partials used by this page partials := []string{"header"} // Render the template - return lib.RenderTemplate(c.Response().Writer, "base", partials, props) + return lib.RenderTemplate(c, "base", partials, props) } diff --git a/pages/register.go b/pages/register.go index 00b3f2b..e556cb6 100644 --- a/pages/register.go +++ b/pages/register.go @@ -10,11 +10,11 @@ type RegisterProps struct { } func Register(c echo.Context) error { - props := HomeProps{} + props := RegisterProps{} // Specify the partials used by this page partials := []string{"header"} // Render the template - return lib.RenderTemplate(c.Response().Writer, "base", partials, props) + return lib.RenderTemplate(c, "base", partials, props) } diff --git a/pages/room.go b/pages/room.go index fd7bb30..def4aa2 100644 --- a/pages/room.go +++ b/pages/room.go @@ -7,8 +7,7 @@ import ( ) type RoomProps struct { - IsLoggedIn bool - Room lib.Room + Room lib.Room } func Room(c echo.Context) error { @@ -28,13 +27,12 @@ func Room(c echo.Context) error { } props := RoomProps{ - IsLoggedIn: lib.IsSignedIn(c), - Room: *room, + Room: *room, } // Specify the partials used by this page partials := []string{"header"} // Render the template - return lib.RenderTemplate(c.Response().Writer, "base", partials, props) + return lib.RenderTemplate(c, "base", partials, props) } diff --git a/pages/signin.go b/pages/signin.go index 815af60..4f6b3b4 100644 --- a/pages/signin.go +++ b/pages/signin.go @@ -10,11 +10,11 @@ type SignInProps struct { } func SignIn(c echo.Context) error { - props := HomeProps{} + props := SignInProps{} // Specify the partials used by this page partials := []string{"header"} // Render the template - return lib.RenderTemplate(c.Response().Writer, "base", partials, props) + return lib.RenderTemplate(c, "base", partials, props) } diff --git a/pages/templates/dashboard.html b/pages/templates/dashboard.html index 7847d6a..f99b83b 100644 --- a/pages/templates/dashboard.html +++ b/pages/templates/dashboard.html @@ -13,7 +13,7 @@ Pollo // Dashboard {{define "main"}}

- Hi, {{.Name}}! + Hi, {{.Props.Name}}!

diff --git a/pages/templates/layouts/base.html b/pages/templates/layouts/base.html index ad9b0a8..7c6bfd7 100644 --- a/pages/templates/layouts/base.html +++ b/pages/templates/layouts/base.html @@ -9,10 +9,8 @@ {{template "head" .}} - {{template "header" .}} -
{{template "main" .}}
diff --git a/pages/templates/partials/buttonlinks.html b/pages/templates/partials/buttonlinks.html deleted file mode 100644 index 64f5818..0000000 --- a/pages/templates/partials/buttonlinks.html +++ /dev/null @@ -1,15 +0,0 @@ -{{define "buttonlinks"}} -{{if eq true .Internal}} - - - {{.Name}} - - -{{else}} - - - {{.Name}} - - -{{end}} -{{end}} diff --git a/pages/templates/partials/cardlinks.html b/pages/templates/partials/cardlinks.html deleted file mode 100644 index 3d04a48..0000000 --- a/pages/templates/partials/cardlinks.html +++ /dev/null @@ -1,54 +0,0 @@ -{{define "cardlinks"}} -
-
-

{{.Name}}

- - {{if .Description}} -

{{.Description}}

- {{end}} - - {{if .Date}} -

-

- - {{.Date}} -
-

- {{end}} - - {{if .Tags}} -
- {{range .Tags}} -
#{{.}}
- {{end}} -
- {{end}} - - {{if .Href}} -
- {{if eq true .Internal}} - - - - {{else}} - - - - {{end}} -
- {{end}} -
-
-{{end}} diff --git a/pages/templates/partials/iconlinks.html b/pages/templates/partials/iconlinks.html deleted file mode 100644 index da882e7..0000000 --- a/pages/templates/partials/iconlinks.html +++ /dev/null @@ -1,5 +0,0 @@ -{{define "iconlinks"}} - - {{.Icon}} - -{{end}} \ No newline at end of file