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 (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
templatefs "pollo/pages/templates"
|
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
|
// Get the name of the current file
|
||||||
_, filename, _, _ := runtime.Caller(1)
|
_, filename, _, _ := runtime.Caller(1)
|
||||||
page := filepath.Base(filename)
|
page := filepath.Base(filename)
|
||||||
|
@ -31,8 +37,15 @@ func RenderTemplate(w http.ResponseWriter, layout string, partials []string, pro
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute the layout template
|
// Wrap the props with the IsLoggedIn status
|
||||||
err = ts.ExecuteTemplate(w, layout, props)
|
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 {
|
if err != nil {
|
||||||
LogError.Print(err.Error())
|
LogError.Print(err.Error())
|
||||||
return err
|
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 {
|
func AuthenticatedEndpointMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
isSignedIn := IsSignedIn(c)
|
isSignedIn := IsSignedIn(c)
|
||||||
|
|
5
main.go
5
main.go
|
@ -55,6 +55,7 @@ func main() {
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
publicPageRoute := e.Group("")
|
publicPageRoute := e.Group("")
|
||||||
protectedPageRoute := e.Group("", lib.AuthenticatedPageMiddleware)
|
protectedPageRoute := e.Group("", lib.AuthenticatedPageMiddleware)
|
||||||
|
authFlowPageRoute := e.Group("", lib.AuthFlowPageMiddleware)
|
||||||
publicApiRoute := e.Group("/api")
|
publicApiRoute := e.Group("/api")
|
||||||
protectedApiRoute := e.Group("/api", lib.AuthenticatedEndpointMiddleware)
|
protectedApiRoute := e.Group("/api", lib.AuthenticatedEndpointMiddleware)
|
||||||
webhookGroup := e.Group("/webhook")
|
webhookGroup := e.Group("/webhook")
|
||||||
|
@ -85,8 +86,8 @@ func main() {
|
||||||
// Page Routes:
|
// Page Routes:
|
||||||
// ------------------------------
|
// ------------------------------
|
||||||
publicPageRoute.GET("/", pages.Home)
|
publicPageRoute.GET("/", pages.Home)
|
||||||
publicPageRoute.GET("/signin", pages.SignIn)
|
authFlowPageRoute.GET("/signin", pages.SignIn)
|
||||||
publicPageRoute.GET("/register", pages.Register)
|
authFlowPageRoute.GET("/register", pages.Register)
|
||||||
protectedPageRoute.GET("/dashboard", pages.Dashboard)
|
protectedPageRoute.GET("/dashboard", pages.Dashboard)
|
||||||
protectedPageRoute.GET("/room/:id", pages.Room)
|
protectedPageRoute.GET("/room/:id", pages.Room)
|
||||||
|
|
||||||
|
|
|
@ -7,24 +7,22 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type DashboardProps struct {
|
type DashboardProps struct {
|
||||||
IsLoggedIn bool
|
Name string
|
||||||
Name string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Dashboard(c echo.Context) error {
|
func Dashboard(c echo.Context) error {
|
||||||
currentSession, error := lib.GetSessionCookie(c.Request(), "session")
|
currentSession, error := lib.GetSessionCookie(c.Request(), "session")
|
||||||
if error != nil {
|
if error != nil {
|
||||||
return c.Redirect(302, "/signin")
|
lib.LogError.Printf("Error getting session: %v", error)
|
||||||
}
|
}
|
||||||
|
|
||||||
props := DashboardProps{
|
props := DashboardProps{
|
||||||
IsLoggedIn: lib.IsSignedIn(c),
|
Name: currentSession.Name,
|
||||||
Name: currentSession.Name,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Specify the partials used by this page
|
// Specify the partials used by this page
|
||||||
partials := []string{"header"}
|
partials := []string{"header"}
|
||||||
|
|
||||||
// Render the template
|
// 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
|
package pages
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
"pollo/lib"
|
"pollo/lib"
|
||||||
|
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ExampleProps struct {
|
type ExampleProps struct {
|
||||||
|
@ -18,5 +19,5 @@ func Example(c echo.Context) error {
|
||||||
partials := []string{"header", "navitems"}
|
partials := []string{"header", "navitems"}
|
||||||
|
|
||||||
// Render the template
|
// 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 {
|
type HomeProps struct {
|
||||||
IsLoggedIn bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Home(c echo.Context) error {
|
func Home(c echo.Context) error {
|
||||||
props := HomeProps{
|
props := HomeProps{}
|
||||||
IsLoggedIn: lib.IsSignedIn(c),
|
|
||||||
}
|
|
||||||
|
|
||||||
println("Home page props: ", props.IsLoggedIn)
|
|
||||||
|
|
||||||
// Specify the partials used by this page
|
// Specify the partials used by this page
|
||||||
partials := []string{"header"}
|
partials := []string{"header"}
|
||||||
|
|
||||||
// Render the template
|
// 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 {
|
func Register(c echo.Context) error {
|
||||||
props := HomeProps{}
|
props := RegisterProps{}
|
||||||
|
|
||||||
// Specify the partials used by this page
|
// Specify the partials used by this page
|
||||||
partials := []string{"header"}
|
partials := []string{"header"}
|
||||||
|
|
||||||
// Render the template
|
// 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 {
|
type RoomProps struct {
|
||||||
IsLoggedIn bool
|
Room lib.Room
|
||||||
Room lib.Room
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Room(c echo.Context) error {
|
func Room(c echo.Context) error {
|
||||||
|
@ -28,13 +27,12 @@ func Room(c echo.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
props := RoomProps{
|
props := RoomProps{
|
||||||
IsLoggedIn: lib.IsSignedIn(c),
|
Room: *room,
|
||||||
Room: *room,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Specify the partials used by this page
|
// Specify the partials used by this page
|
||||||
partials := []string{"header"}
|
partials := []string{"header"}
|
||||||
|
|
||||||
// Render the template
|
// 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 {
|
func SignIn(c echo.Context) error {
|
||||||
props := HomeProps{}
|
props := SignInProps{}
|
||||||
|
|
||||||
// Specify the partials used by this page
|
// Specify the partials used by this page
|
||||||
partials := []string{"header"}
|
partials := []string{"header"}
|
||||||
|
|
||||||
// Render the template
|
// 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"}}
|
{{define "main"}}
|
||||||
<div class="flex flex-col items-center justify-center gap-8">
|
<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">
|
<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>
|
</h1>
|
||||||
<!-- Form to create a new room -->
|
<!-- 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()">
|
<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...">
|
<meta name="description" content="Just here for the vibes...">
|
||||||
{{template "head" .}}
|
{{template "head" .}}
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body class="block h-[100%]">
|
<body class="block h-[100%]">
|
||||||
{{template "header" .}}
|
{{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)]">
|
<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" .}}
|
{{template "main" .}}
|
||||||
</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