Fixed auth flow
This commit is contained in:
parent
b9bd61026c
commit
c171b417d8
14 changed files with 49 additions and 107 deletions
|
@ -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
|
||||
|
|
12
lib/user.go
12
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)
|
||||
|
|
5
main.go
5
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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ Pollo // Dashboard
|
|||
{{define "main"}}
|
||||
<div class="flex flex-col items-center justify-center gap-8">
|
||||
<h1 class="flex flex-row flex-wrap text-center justify-center items-center gap-1 text-4xl font-bold">
|
||||
Hi, {{.Name}}!
|
||||
Hi, {{.Props.Name}}!
|
||||
</h1>
|
||||
<!-- Form to create a new room -->
|
||||
<form class="flex flex-col gap-2" method="POST" hx-post="/api/room" hx-target="#room-list" hx-swap="outerHTML" hx-on--after-request="this.reset()">
|
||||
|
|
|
@ -9,10 +9,8 @@
|
|||
<meta name="description" content="Just here for the vibes...">
|
||||
{{template "head" .}}
|
||||
</head>
|
||||
|
||||
<body class="block h-[100%]">
|
||||
{{template "header" .}}
|
||||
|
||||
<main class="container flex flex-col items-center justify-center gap-3 sm:gap-6 p-4 text-center mx-auto min-h-[calc(100%-64px)]">
|
||||
{{template "main" .}}
|
||||
</main>
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
{{define "buttonlinks"}}
|
||||
{{if eq true .Internal}}
|
||||
|
||||
<a class="btn btn-primary btn-outline btn-md lg:btn-lg" href={{.Href}}>
|
||||
{{.Name}}
|
||||
</a>
|
||||
|
||||
{{else}}
|
||||
|
||||
<a class="btn btn-primary btn-outline btn-md lg:btn-lg" href={{.Href}} target="_blank" rel="noreferrer">
|
||||
{{.Name}}
|
||||
</a>
|
||||
|
||||
{{end}}
|
||||
{{end}}
|
|
@ -1,54 +0,0 @@
|
|||
{{define "cardlinks"}}
|
||||
<div class="card card-compact w-64 bg-secondary shadow-xl">
|
||||
<div class="card-body text-base-100 flex flex-col">
|
||||
<h2 class="card-title text-base-100">{{.Name}}</h2>
|
||||
|
||||
{{if .Description}}
|
||||
<p>{{.Description}}</p>
|
||||
{{end}}
|
||||
|
||||
{{if .Date}}
|
||||
<p>
|
||||
<div class="flex flex-row flex-wrap items-center gap-1 text-md">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-clock-4"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>
|
||||
{{.Date}}
|
||||
</div>
|
||||
</p>
|
||||
{{end}}
|
||||
|
||||
{{if .Tags}}
|
||||
<div class="flex flex-row flex-wrap text-center items-center justify-center gap-1">
|
||||
{{range .Tags}}
|
||||
<div class="badge badge-accent">#{{.}}</div>
|
||||
{{end}}
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
{{if .Href}}
|
||||
<div class="card-actions justify-end">
|
||||
{{if eq true .Internal}}
|
||||
<a
|
||||
role="button"
|
||||
href={{.Href}}
|
||||
aria-label={{.Name}}
|
||||
class="btn btn-circle btn-base-100 text-primary hover:btn-accent hover:text-neutral"
|
||||
>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-arrow-right"><path d="M5 12h14"/><path d="m12 5 7 7-7 7"/></svg>
|
||||
</a>
|
||||
{{else}}
|
||||
<a
|
||||
role="button"
|
||||
href={{.Href}}
|
||||
aria-label={{.Name}}
|
||||
class="btn btn-circle btn-base-100 text-primary hover:btn-accent hover:text-neutral"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/></svg>
|
||||
</a>
|
||||
{{end}}
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
|
@ -1,5 +0,0 @@
|
|||
{{define "iconlinks"}}
|
||||
<a class="fill-white hover:fill-pink-500" href={{.Href}} target="_blank" rel="me" aria-label={{.Name}}>
|
||||
{{.Icon}}
|
||||
</a>
|
||||
{{end}}
|
Loading…
Add table
Reference in a new issue