diff --git a/auth-notes.org b/auth-notes.org index a1512f9..6b644ef 100644 --- a/auth-notes.org +++ b/auth-notes.org @@ -50,10 +50,20 @@ so, yup. coupling between js code of oauth, middlewares and body tag. this seems but it somewhat works -** TODO add one more page that checks auth +** DONE add one more page that checks auth and let's use existing middleware from framework documentation + +with hx-boost things are well, +but i also need header as fragment, so that opening in new tab would work. +and all js imports and libraries that are required by all pages, should be in all templates + +** DONE i suppose there has to be a base template then +and now all since base template has Nav, +i need to provide attibutes which are used there, huh +well. hmmmmm. yeah, i guess ** TODO add tailwind styling ** TODO package static into single binary +i guess already done? ** TODO write nix build ** TODO write nixos module ** TODO add docker image from nix @@ -61,3 +71,7 @@ and let's use existing middleware from framework documentation ** TODO add readme and comments ** TODO configure tls / ssl / https on franzk deployment can it be configured on render.com? +** TODO maybe add middleware so that 401 would be a page, and not json +** TODO i guess i'll want a makefile? +then wgo could be build with makefile and run +and nix packaging could be more straightforward, and not too prohibitive to those who don't use nix diff --git a/pages/pageRoutes.go b/pages/pageRoutes.go index 6d6862b..c323c37 100644 --- a/pages/pageRoutes.go +++ b/pages/pageRoutes.go @@ -5,6 +5,7 @@ import ( "embed" "fmt" "html/template" + "math/rand" "net/http" "github.com/labstack/echo/v5" @@ -21,21 +22,28 @@ var staticFilesFS embed.FS func AddPageRoutes(app *pocketbase.PocketBase) { app.OnBeforeServe().Add(getIndexPageRoute(app)) + app.OnBeforeServe().Add(somePageRoute) app.OnBeforeServe().Add(func(e *core.ServeEvent) error { e.Router.StaticFS("/static", staticFilesFS) // this path works : http://127.0.0.1:8090/static/static/public/htmx.min.js - return nil + return nil }) } -// render and return index page -func getIndexPageRoute(app *pocketbase.PocketBase) func(*core.ServeEvent) error { - return func (e *core.ServeEvent) error { +type navInfo struct { + Username string + IsGuest bool + EnabledOauthProviders []string +} + +// render and return some page +func getIndexPageRoute(app *pocketbase.PocketBase) func(*core.ServeEvent) error { + return func(e *core.ServeEvent) error { e.Router.GET("/", func(c echo.Context) error { // first collect data - info := apis.RequestInfo(c) - admin := info.Admin // nil if not authenticated as admin + info := apis.RequestInfo(c) + admin := info.Admin // nil if not authenticated as admin record := info.AuthRecord // nil if not authenticated as regular auth record isGuest := admin == nil && record == nil @@ -60,19 +68,22 @@ func getIndexPageRoute(app *pocketbase.PocketBase) func(*core.ServeEvent) error fmt.Printf(">> enabled providers names %+v\n", oauthProviderNames) indexPageData := struct { - IsGuest, IsAdmin bool - Username string + IsGuest, IsAdmin bool + Username string EnabledOauthProviders []string + NavInfo navInfo }{ - IsGuest: isGuest, IsAdmin: admin != nil, - Username: username, - EnabledOauthProviders: oauthProviderNames, + NavInfo: navInfo{ + IsGuest: isGuest, + Username: username, + EnabledOauthProviders: oauthProviderNames, + }, } // then render template with it templateName := "templates/index.gohtml" - tmpl := template.Must(template.ParseFS(templatesFS, templateName)) + tmpl := template.Must(template.ParseFS(templatesFS, "templates/base.gohtml", templateName)) var instantiatedTemplate bytes.Buffer if err := tmpl.Execute(&instantiatedTemplate, indexPageData); err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"message": "error parsing template"}) @@ -83,3 +94,55 @@ func getIndexPageRoute(app *pocketbase.PocketBase) func(*core.ServeEvent) error return nil } } + +const charset = "abcdefghijklmnopqrstuvwxyz" + + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + +func stringWithCharset(length int, charset string) string { + b := make([]byte, length) + for i := range b { + b[i] = charset[rand.Intn(len(charset))] + } + return string(b) +} +func somePageRoute(e *core.ServeEvent) error { + e.Router.GET("/somepage", func(c echo.Context) error { + // get data + // and since i'm using 'base.gohtml' with Nav, i'll need Nav info + + info := apis.RequestInfo(c) + admin := info.Admin // nil if not authenticated as admin + record := info.AuthRecord // nil if not authenticated as regular auth record + + username := "" + switch { + case admin != nil: + username = admin.Email + case record != nil: + username = record.Username() + } + + somePageData := struct { + RandomNumber int + RandomString string + NavInfo navInfo + }{ + RandomNumber: rand.Int(), + RandomString: stringWithCharset(25, charset), + NavInfo: navInfo{ + Username: username, + }, + } + + // then render template with it + templateName := "templates/somepage.gohtml" + tmpl := template.Must(template.ParseFS(templatesFS, "templates/base.gohtml", templateName)) + var instantiatedTemplate bytes.Buffer + if err := tmpl.Execute(&instantiatedTemplate, somePageData); err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{"message": "error parsing template"}) + } + + return c.HTML(http.StatusOK, instantiatedTemplate.String()) + }, apis.RequireAdminOrRecordAuth()) + return nil +} diff --git a/pages/templates/base.gohtml b/pages/templates/base.gohtml new file mode 100644 index 0000000..c550127 --- /dev/null +++ b/pages/templates/base.gohtml @@ -0,0 +1,100 @@ + + +
+ + +This is content only for authenticated users! Congratulations!
- {{ end }} -This is content only for authenticated users! Congratulations!
+ + {{ else }} +There will be some content only for authorized users
+ {{ end }} +{{ end }} diff --git a/pages/templates/somepage.gohtml b/pages/templates/somepage.gohtml new file mode 100644 index 0000000..9068da4 --- /dev/null +++ b/pages/templates/somepage.gohtml @@ -0,0 +1,14 @@ +{{ define "title" }} + Some page with content +{{ end }} + +{{ define "content" }} +Will be rendered on server
+and locked under apis.RequireAdminOrRecordAuth default middleware
+here are some random numbers
+