Update URL routes

This commit is contained in:
Tom Wiesing 2023-01-03 13:02:42 +01:00
parent dab7a5c4ae
commit b3039768af
No known key found for this signature in database
21 changed files with 152 additions and 96 deletions

View file

@ -1,7 +1,7 @@
package cli package cli
// =========================================================================================================== // ===========================================================================================================
// This file was generated automatically at 02-01-2023 12:34:04 using gogenlicense. // This file was generated automatically at 03-01-2023 11:39:55 using gogenlicense.
// Do not edit manually, as changes may be overwritten. // Do not edit manually, as changes may be overwritten.
// =========================================================================================================== // ===========================================================================================================
@ -2133,7 +2133,7 @@ package cli
// # Generation // # Generation
// //
// This variable and the associated documentation have been automatically generated using the 'gogenlicense' tool. // This variable and the associated documentation have been automatically generated using the 'gogenlicense' tool.
// It was last updated at 02-01-2023 12:34:04. // It was last updated at 03-01-2023 11:39:55.
var LegalNotices string var LegalNotices string
func init() { func init() {

View file

@ -27,7 +27,7 @@ var (
_ component.Routeable = (*Auth)(nil) _ component.Routeable = (*Auth)(nil)
) )
func (auth *Auth) Routes() []string { return []string{"/auth/"} } func (auth *Auth) Routes() []string { return []string{"/user/"} }
func (auth *Auth) HandleRoute(ctx context.Context, route string) (http.Handler, error) { func (auth *Auth) HandleRoute(ctx context.Context, route string) (http.Handler, error) {
router := httprouter.New() router := httprouter.New()

View file

@ -33,7 +33,7 @@ func (auth *Auth) Protect(handler http.Handler, perm Permission) http.Handler {
} }
// redirect the user to the login endpoint, with the original URI as a return // redirect the user to the login endpoint, with the original URI as a return
dest := "/auth/login?next=" + url.QueryEscape(r.URL.RequestURI()) dest := "/user/login?next=" + url.QueryEscape(r.URL.RequestURI())
http.Redirect(w, r, dest, http.StatusSeeOther) http.Redirect(w, r, dest, http.StatusSeeOther)
return return
} }

View file

@ -49,7 +49,8 @@ func (auth *Auth) authPassword(ctx context.Context) http.Handler {
CSRF: auth.csrf.Get(nil), CSRF: auth.csrf.Get(nil),
RenderTemplate: passwordTemplate, RenderTemplate: passwordTemplate,
RenderTemplateContext: auth.UserFormContext,
Validate: func(r *http.Request, values map[string]string) (struct{}, error) { Validate: func(r *http.Request, values map[string]string) (struct{}, error) {
old, passcode, new, new2 := values["old"], values["passcode"], values["new"], values["new2"] old, passcode, new, new2 := values["old"], values["passcode"], values["new"], values["new2"]

View file

@ -1,20 +1,16 @@
{{ template "_base.html" . }} {{ template "_base.html" . }}
{{ define "title" }}Distillery User{{ end }} {{ define "title" }}User{{ end }}
{{ define "header/time" }}
<!-- no header/time -->
{{ end }}
{{ define "header"}} {{ define "header"}}
<!-- no header --> <p>
<a class="pure-button pure-button-primary" href="/user/">{{ .User.User }}</a>
</p>
<p>
<a class="pure-button pure-button-small" href="/user/logout/">Logout</a>
</p>
{{ end }} {{ end }}
{{ define "content" }} {{ define "content" }}
<div class="pure-u-1">
Welcome {{ .User.User }}!
<a class="pure-button pure-button-small" href="/auth/logout/">Logout</a>
<hr />
</div>
<div class="pure-u-1"> <div class="pure-u-1">
<p> <p>
{{ if .User.Admin }} {{ if .User.Admin }}
@ -29,11 +25,11 @@
{{ end }} {{ end }}
</p> </p>
<div class="pure-button-group" role="group" role="Actions"> <div class="pure-button-group" role="group" role="Actions">
<a class="pure-button" href="/auth/password/">Change Password</a> <a class="pure-button" href="/user/password/">Change Password</a>
{{ if .User.TOTPEnabled }} {{ if .User.TOTPEnabled }}
<a class="pure-button" href="/auth/totp/disable/">Disable TOTP</a> <a class="pure-button" href="/user/totp/disable/">Disable TOTP</a>
{{ else }} {{ else }}
<a class="pure-button" href="/auth/totp/enable/">Enable TOTP</a> <a class="pure-button" href="/user/totp/enable/">Enable TOTP</a>
{{ end }} {{ end }}
</div> </div>
<hr /> <hr />
@ -43,11 +39,13 @@
<div class="pure-u-1"> <div class="pure-u-1">
{{ if (not .User.TOTPEnabled) }} {{ if (not .User.TOTPEnabled) }}
<div> <div>
TOTP is required to access these. <p class="error-message">
TOTP is required to access these.
</p>
</div> </div>
{{ end }} {{ end }}
<div class="pure-button-group" role="group" role="Actions"> <div class="pure-button-group" role="group" role="Actions">
<a class="pure-button" href="/dis/">Distillery Control Page</a> <a class="pure-button" href="/admin/">Distillery Admin Page</a>
</div> </div>
<hr /> <hr />
</div> </div>

View file

@ -1,9 +1,13 @@
{{ template "_form.html" . }} {{ template "_form.html" . }}
{{ define "form/title" }}Change Password{{ end }} {{ define "form/title" }}Change Password{{ end }}
{{ define "form/button" }}Update{{ end }} {{ define "form/button" }}Update{{ end }}
{{ define "form/extra" }}
<div> {{ define "header"}}
<a class="pure-button" href="/auth/">Back</a> <p>
<hr /> <a class="pure-button" href="/user/">{{ .User.User }}</a> &gt;
</div> <a class="pure-button pure-button-primary" href="/user/password/">Change Password</a>
</p>
<p>
<a class="pure-button pure-button-small" href="/user/logout/">Logout</a>
</p>
{{ end }} {{ end }}

View file

@ -1,12 +1,17 @@
{{ template "_form.html" . }} {{ template "_form.html" . }}
{{ define "form/title" }}Disable TOTP{{ end }} {{ define "form/title" }}Disable TOTP{{ end }}
{{ define "form/button" }}Disable{{ end }} {{ define "form/button" }}Disable{{ end }}
{{ define "form/extra" }}
<div> {{ define "header"}}
<a class="pure-button" href="/auth/">Back</a> <p>
<hr /> <a class="pure-button" href="/user/">{{ .User.User }}</a> &gt;
</div> <a class="pure-button pure-button-primary" href="/user/totp/disable/">Disable TOTP</a>
</p>
<p>
<a class="pure-button pure-button-small" href="/user/logout/">Logout</a>
</p>
{{ end }} {{ end }}
{{ define "form/inside" }} {{ define "form/inside" }}
<div> <div>
<ul> <ul>

View file

@ -1,11 +1,14 @@
{{ template "_form.html" . }} {{ template "_form.html" . }}
{{ define "form/title" }}Enable TOTP{{ end }} {{ define "form/title" }}Enable TOTP{{ end }}
{{ define "form/button" }}Enable{{ end }} {{ define "form/button" }}Enable{{ end }}
{{ define "form/extra" }} {{ define "header"}}
<div> <p>
<a class="pure-button" href="/auth/">Back</a> <a class="pure-button" href="/user/">{{ .User.User }}</a> &gt;
<hr /> <a class="pure-button pure-button-primary" href="/user/totp/enable/">Enable TOTP</a>
</div> </p>
<p>
<a class="pure-button pure-button-small" href="/user/logout/">Logout</a>
</p>
{{ end }} {{ end }}
{{ define "form/inside" }} {{ define "form/inside" }}
<div> <div>

View file

@ -1,11 +1,14 @@
{{ template "_form.html" . }} {{ template "_form.html" . }}
{{ define "form/title" }}Enable TOTP{{ end }} {{ define "form/title" }}Enable TOTP{{ end }}
{{ define "form/button" }}Enable{{ end }} {{ define "form/button" }}Enable{{ end }}
{{ define "form/extra" }} {{ define "header" }}
<div> <p>
<a class="pure-button" href="/auth/">Back</a> <a class="pure-button" href="/user/">{{ .User.User }}</a> &gt;
<hr /> <a class="pure-button pure-button-primary" href="/user/totp/enroll/">Enroll TOTP</a>
</div> </p>
<p>
<a class="pure-button pure-button-small" href="/user/logout/">Logout</a>
</p>
{{ end }} {{ end }}
{{ define "form/inside" }} {{ define "form/inside" }}
<div> <div>

View file

@ -28,7 +28,9 @@ func (auth *Auth) authTOTPEnable(ctx context.Context) http.Handler {
user, err := auth.UserOf(r) user, err := auth.UserOf(r)
return struct{}{}, err == nil && user != nil && user.TOTPEnabled return struct{}{}, err == nil && user != nil && user.TOTPEnabled
}, },
RenderTemplate: totpEnableTemplate,
RenderTemplate: totpEnableTemplate,
RenderTemplateContext: auth.UserFormContext,
Validate: func(r *http.Request, values map[string]string) (struct{}, error) { Validate: func(r *http.Request, values map[string]string) (struct{}, error) {
password := values["password"] password := values["password"]
@ -55,7 +57,7 @@ func (auth *Auth) authTOTPEnable(ctx context.Context) http.Handler {
}, },
RenderSuccess: func(_ struct{}, values map[string]string, w http.ResponseWriter, r *http.Request) error { RenderSuccess: func(_ struct{}, values map[string]string, w http.ResponseWriter, r *http.Request) error {
http.Redirect(w, r, "/auth/totp/enroll", http.StatusSeeOther) http.Redirect(w, r, "/user/totp/enroll", http.StatusSeeOther)
return nil return nil
}, },
} }
@ -66,7 +68,7 @@ var totpEnrollStr string
var totpEnrollTemplate = static.AssetsAuthLogin.MustParseShared("totp_enroll.html", totpEnrollStr) var totpEnrollTemplate = static.AssetsAuthLogin.MustParseShared("totp_enroll.html", totpEnrollStr)
type totpEnrollContext struct { type totpEnrollContext struct {
httpx.FormContext userFormContext
TOTPImage template.URL TOTPImage template.URL
TOTPURL template.URL TOTPURL template.URL
} }
@ -86,11 +88,16 @@ func (auth *Auth) authTOTPEnroll(ctx context.Context) http.Handler {
return struct{}{}, user != nil && user.TOTPEnabled return struct{}{}, user != nil && user.TOTPEnabled
}, },
RenderForm: func(context httpx.FormContext, w http.ResponseWriter, r *http.Request) { RenderForm: func(context httpx.FormContext, w http.ResponseWriter, r *http.Request) {
user, err := auth.UserOf(r)
ctx := totpEnrollContext{ ctx := totpEnrollContext{
FormContext: context, userFormContext: userFormContext{
FormContext: context,
},
} }
if user, err := auth.UserOf(r); err == nil && user != nil { if err == nil && user != nil {
ctx.userFormContext.User = &user.User
secret, err := user.TOTP() secret, err := user.TOTP()
if err == nil { if err == nil {
img, _ := TOTPLink(secret, 500, 500) img, _ := TOTPLink(secret, 500, 500)
@ -127,7 +134,7 @@ func (auth *Auth) authTOTPEnroll(ctx context.Context) http.Handler {
}, },
RenderSuccess: func(_ struct{}, values map[string]string, w http.ResponseWriter, r *http.Request) error { RenderSuccess: func(_ struct{}, values map[string]string, w http.ResponseWriter, r *http.Request) error {
http.Redirect(w, r, "/auth/", http.StatusSeeOther) http.Redirect(w, r, "/user/", http.StatusSeeOther)
return nil return nil
}, },
} }
@ -151,7 +158,8 @@ func (auth *Auth) authTOTPDisable(ctx context.Context) http.Handler {
user, _ := auth.UserOf(r) user, _ := auth.UserOf(r)
return struct{}{}, user != nil && !user.TOTPEnabled return struct{}{}, user != nil && !user.TOTPEnabled
}, },
RenderTemplate: totpDisableTemplate, RenderTemplate: totpDisableTemplate,
RenderTemplateContext: auth.UserFormContext,
Validate: func(r *http.Request, values map[string]string) (struct{}, error) { Validate: func(r *http.Request, values map[string]string) (struct{}, error) {
password, passcode := values["password"], values["passcode"] password, passcode := values["password"], values["passcode"]
@ -178,7 +186,7 @@ func (auth *Auth) authTOTPDisable(ctx context.Context) http.Handler {
}, },
RenderSuccess: func(_ struct{}, values map[string]string, w http.ResponseWriter, r *http.Request) error { RenderSuccess: func(_ struct{}, values map[string]string, w http.ResponseWriter, r *http.Request) error {
http.Redirect(w, r, "/auth/", http.StatusSeeOther) http.Redirect(w, r, "/user/", http.StatusSeeOther)
return nil return nil
}, },
} }

View file

@ -7,8 +7,10 @@ import (
"errors" "errors"
"fmt" "fmt"
"image/png" "image/png"
"net/http"
"github.com/FAU-CDI/wisski-distillery/internal/models" "github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
"github.com/pquerna/otp" "github.com/pquerna/otp"
"github.com/pquerna/otp/totp" "github.com/pquerna/otp/totp"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
@ -288,3 +290,18 @@ func (au *AuthUser) Delete(ctx context.Context) error {
return table.Delete(&au.User).Error return table.Delete(&au.User).Error
} }
type userFormContext struct {
httpx.FormContext
User *models.User
}
func (au *Auth) UserFormContext(ctx httpx.FormContext, r *http.Request) any {
user, err := au.UserOf(r)
uctx := userFormContext{FormContext: ctx}
if err == nil {
uctx.User = &user.User
}
return uctx
}

View file

@ -1,10 +1,10 @@
{{ template "_base.html" . }} {{ template "_base.html" . }}
{{ define "title" }}Distillery Control Page - Components Page{{ end }} {{ define "title" }}Distillery Admin - Components Page{{ end }}
{{ define "header"}} {{ define "header"}}
<p> <p>
<a class="pure-button" href="/dis/index">Control</a> &gt; <a class="pure-button" href="/admin/index">Admin</a> &gt;
<a class="pure-button pure-button-primary" href="/dis/components">Components</a> <a class="pure-button pure-button-primary" href="/admin/components">Components</a>
</p> </p>
{{ end }} {{ end }}

View file

@ -1,12 +1,12 @@
{{ template "_base.html" . }} {{ template "_base.html" . }}
{{ define "title" }}Distillery Control Page{{ end }} {{ define "title" }}Distillery Admin{{ end }}
{{ define "header"}} {{ define "header"}}
<p> <p>
<a class="pure-button pure-button-primary" href="/dis/index">Control</a> <a class="pure-button pure-button-primary" href="/admin/index">Admin</a>
</p> </p>
<p> <p>
<a class="pure-button" href="/dis/components">Components</a> <a class="pure-button" href="/admin/components">Components</a>
</p> </p>
{{ end }} {{ end }}
@ -269,7 +269,7 @@
<p> <p>
<a href="{{.URL}}" target="_blank" rel="noopener noreferrer">{{.URL}}</a><br> <a href="{{.URL}}" target="_blank" rel="noopener noreferrer">{{.URL}}</a><br>
<a class="pure-button" href="/dis/instance/{{.Slug}}">Details</a> <a class="pure-button" href="/admin/instance/{{.Slug}}">Details</a>
</p> </p>
</div> </div>
</div> </div>

View file

@ -1,11 +1,11 @@
{{ template "_base.html" . }} {{ template "_base.html" . }}
{{ define "title" }}Distillery Control Page - {{ .Instance.Slug }} - Ingredients{{ end }} {{ define "title" }}Distillery Admin - {{ .Instance.Slug }} - Ingredients{{ end }}
{{ define "header"}} {{ define "header"}}
<p> <p>
<a class="pure-button" href="/dis/index">Control</a> &gt; <a class="pure-button" href="/admin/index">Admin</a> &gt;
<a class="pure-button" href="/dis/instance/{{ .Instance.Slug }}">Instance</a> &gt; <a class="pure-button" href="/admin/instance/{{ .Instance.Slug }}">Instance</a> &gt;
<a class="pure-button pure-button-primary" href="/dis/ingredients/{{ .Instance.Slug }}">Ingredients</a> <a class="pure-button pure-button-primary" href="/admin/ingredients/{{ .Instance.Slug }}">Ingredients</a>
</p> </p>
{{ end }} {{ end }}

View file

@ -1,13 +1,13 @@
{{ template "_base.html" . }} {{ template "_base.html" . }}
{{ define "title" }}Distillery Control Page - {{ .Info.Slug }}{{ end }} {{ define "title" }}Distillery Admin - {{ .Info.Slug }}{{ end }}
{{ define "header"}} {{ define "header"}}
<p> <p>
<a class="pure-button" href="/dis/index">Control</a> &gt; <a class="pure-button" href="/admin/index">Control</a> &gt;
<a class="pure-button pure-button-primary" href="/dis/instance/{{ .Info.Slug }}">Instance</a> <a class="pure-button pure-button-primary" href="/admin/instance/{{ .Info.Slug }}">Instance</a>
</p> </p>
<p> <p>
<a class="pure-button" href="/dis/ingredients/{{ .Info.Slug }}">Ingredients</a> <a class="pure-button" href="/admin/ingredients/{{ .Info.Slug }}">Ingredients</a>
</p> </p>
{{ end }} {{ end }}
@ -247,7 +247,7 @@
<code class="date">{{ $user.Login.Time.Format "2006-01-02T15:04:05Z07:00" }}</code> <code class="date">{{ $user.Login.Time.Format "2006-01-02T15:04:05Z07:00" }}</code>
</td> </td>
<td> <td>
<form action="/dis/api/login" method="POST" target="_blank"> <form action="/admin/api/login" method="POST" target="_blank">
<input type="hidden" name="slug" value="{{ $slug }}"> <input type="hidden" name="slug" value="{{ $slug }}">
<input type="hidden" name="user" value="{{ $user.Name }}"> <input type="hidden" name="user" value="{{ $user.Name }}">
<input type="submit" class="pure-button pure-button-action" value="Login in new window"> <input type="submit" class="pure-button pure-button-action" value="Login in new window">

View file

@ -35,7 +35,7 @@ var (
_ component.Routeable = (*Info)(nil) _ component.Routeable = (*Info)(nil)
) )
func (*Info) Routes() []string { return []string{"/dis/"} } func (*Info) Routes() []string { return []string{"/admin/"} }
func (info *Info) HandleRoute(ctx context.Context, route string) (handler http.Handler, err error) { func (info *Info) HandleRoute(ctx context.Context, route string) (handler http.Handler, err error) {

View file

@ -5,47 +5,47 @@ package static
// AssetsHomeHome contains assets for the 'HomeHome' entrypoint. // AssetsHomeHome contains assets for the 'HomeHome' entrypoint.
var AssetsHomeHome = Assets{ var AssetsHomeHome = Assets{
Scripts: `<script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script><script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script>`, Scripts: `<script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script><script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script>`,
Styles: `<link rel="stylesheet" href="/static/HomeHome.25c6db8a.css"><link rel="stylesheet" href="/static/HomeHome.2353e048.css">`, Styles: `<link rel="stylesheet" href="/static/HomeHome.4ec77c43.css"><link rel="stylesheet" href="/static/HomeHome.2353e048.css">`,
} }
// AssetsComponentsIndex contains assets for the 'ComponentsIndex' entrypoint. // AssetsComponentsIndex contains assets for the 'ComponentsIndex' entrypoint.
var AssetsComponentsIndex = Assets{ var AssetsComponentsIndex = Assets{
Scripts: `<script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script><script type="module" src="/static/ComponentsIndex.38d394c2.js"></script><script src="/static/ComponentsIndex.38d394c2.js" nomodule="" defer></script>`, Scripts: `<script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script><script type="module" src="/static/ComponentsIndex.38d394c2.js"></script><script src="/static/ComponentsIndex.38d394c2.js" nomodule="" defer></script>`,
Styles: `<link rel="stylesheet" href="/static/HomeHome.25c6db8a.css"><link rel="stylesheet" href="/static/ComponentsIndex.38d394c2.css">`, Styles: `<link rel="stylesheet" href="/static/HomeHome.4ec77c43.css"><link rel="stylesheet" href="/static/ComponentsIndex.38d394c2.css">`,
} }
// AssetsControlIndex contains assets for the 'ControlIndex' entrypoint. // AssetsControlIndex contains assets for the 'ControlIndex' entrypoint.
var AssetsControlIndex = Assets{ var AssetsControlIndex = Assets{
Scripts: `<script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script><script type="module" src="/static/ControlIndex.a72fc239.js"></script><script src="/static/ControlIndex.75d2a312.js" nomodule="" defer></script>`, Scripts: `<script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script><script type="module" src="/static/ControlIndex.a72fc239.js"></script><script src="/static/ControlIndex.75d2a312.js" nomodule="" defer></script>`,
Styles: `<link rel="stylesheet" href="/static/HomeHome.25c6db8a.css"><link rel="stylesheet" href="/static/ControlIndex.6d59e220.css"><link rel="stylesheet" href="/static/ControlIndex.6d2ae968.css">`, Styles: `<link rel="stylesheet" href="/static/HomeHome.4ec77c43.css"><link rel="stylesheet" href="/static/ControlIndex.6d59e220.css"><link rel="stylesheet" href="/static/ControlIndex.6d2ae968.css">`,
} }
// AssetsControlInstance contains assets for the 'ControlInstance' entrypoint. // AssetsControlInstance contains assets for the 'ControlInstance' entrypoint.
var AssetsControlInstance = Assets{ var AssetsControlInstance = Assets{
Scripts: `<script nomodule="" defer src="/static/ControlIndex.75d2a312.js"></script><script type="module" src="/static/ControlIndex.a72fc239.js"></script><script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script><script type="module" src="/static/ControlInstance.66b95713.js"></script><script src="/static/ControlInstance.9cc7166d.js" nomodule="" defer></script>`, Scripts: `<script nomodule="" defer src="/static/ControlIndex.75d2a312.js"></script><script type="module" src="/static/ControlIndex.a72fc239.js"></script><script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script><script type="module" src="/static/ControlInstance.66b95713.js"></script><script src="/static/ControlInstance.9cc7166d.js" nomodule="" defer></script>`,
Styles: `<link rel="stylesheet" href="/static/HomeHome.25c6db8a.css"><link rel="stylesheet" href="/static/ControlIndex.6d59e220.css"><link rel="stylesheet" href="/static/ControlIndex.6d2ae968.css"><link rel="stylesheet" href="/static/ControlInstance.38d394c2.css">`, Styles: `<link rel="stylesheet" href="/static/HomeHome.4ec77c43.css"><link rel="stylesheet" href="/static/ControlIndex.6d59e220.css"><link rel="stylesheet" href="/static/ControlIndex.6d2ae968.css"><link rel="stylesheet" href="/static/ControlInstance.38d394c2.css">`,
} }
// AssetsInstanceComponentsIndex contains assets for the 'InstanceComponentsIndex' entrypoint. // AssetsInstanceComponentsIndex contains assets for the 'InstanceComponentsIndex' entrypoint.
var AssetsInstanceComponentsIndex = Assets{ var AssetsInstanceComponentsIndex = Assets{
Scripts: `<script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script><script type="module" src="/static/InstanceComponentsIndex.38d394c2.js"></script><script src="/static/InstanceComponentsIndex.38d394c2.js" nomodule="" defer></script>`, Scripts: `<script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script><script type="module" src="/static/InstanceComponentsIndex.38d394c2.js"></script><script src="/static/InstanceComponentsIndex.38d394c2.js" nomodule="" defer></script>`,
Styles: `<link rel="stylesheet" href="/static/HomeHome.25c6db8a.css"><link rel="stylesheet" href="/static/InstanceComponentsIndex.38d394c2.css">`, Styles: `<link rel="stylesheet" href="/static/HomeHome.4ec77c43.css"><link rel="stylesheet" href="/static/InstanceComponentsIndex.38d394c2.css">`,
} }
// AssetsAuthLogin contains assets for the 'AuthLogin' entrypoint. // AssetsAuthLogin contains assets for the 'AuthLogin' entrypoint.
var AssetsAuthLogin = Assets{ var AssetsAuthLogin = Assets{
Scripts: `<script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script><script type="module" src="/static/AuthLogin.38d394c2.js"></script><script src="/static/AuthLogin.38d394c2.js" nomodule="" defer></script>`, Scripts: `<script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script><script type="module" src="/static/AuthLogin.38d394c2.js"></script><script src="/static/AuthLogin.38d394c2.js" nomodule="" defer></script>`,
Styles: `<link rel="stylesheet" href="/static/HomeHome.25c6db8a.css"><link rel="stylesheet" href="/static/AuthLogin.38d394c2.css">`, Styles: `<link rel="stylesheet" href="/static/HomeHome.4ec77c43.css"><link rel="stylesheet" href="/static/AuthLogin.38d394c2.css">`,
} }
// AssetsAuthHome contains assets for the 'AuthHome' entrypoint. // AssetsAuthHome contains assets for the 'AuthHome' entrypoint.
var AssetsAuthHome = Assets{ var AssetsAuthHome = Assets{
Scripts: `<script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script><script type="module" src="/static/AuthHome.38d394c2.js"></script><script src="/static/AuthHome.38d394c2.js" nomodule="" defer></script>`, Scripts: `<script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script><script type="module" src="/static/AuthHome.38d394c2.js"></script><script src="/static/AuthHome.38d394c2.js" nomodule="" defer></script>`,
Styles: `<link rel="stylesheet" href="/static/HomeHome.25c6db8a.css"><link rel="stylesheet" href="/static/AuthHome.38d394c2.css">`, Styles: `<link rel="stylesheet" href="/static/HomeHome.4ec77c43.css"><link rel="stylesheet" href="/static/AuthHome.38d394c2.css">`,
} }
// AssetsAuthTOTP contains assets for the 'AuthTOTP' entrypoint. // AssetsAuthTOTP contains assets for the 'AuthTOTP' entrypoint.
var AssetsAuthTOTP = Assets{ var AssetsAuthTOTP = Assets{
Scripts: `<script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script><script type="module" src="/static/AuthTOTP.38d394c2.js"></script><script src="/static/AuthTOTP.38d394c2.js" nomodule="" defer></script>`, Scripts: `<script type="module" src="/static/HomeHome.38d394c2.js"></script><script src="/static/HomeHome.38d394c2.js" nomodule="" defer></script><script type="module" src="/static/AuthTOTP.38d394c2.js"></script><script src="/static/AuthTOTP.38d394c2.js" nomodule="" defer></script>`,
Styles: `<link rel="stylesheet" href="/static/HomeHome.25c6db8a.css"><link rel="stylesheet" href="/static/AuthTOTP.38d394c2.css">`, Styles: `<link rel="stylesheet" href="/static/HomeHome.4ec77c43.css"><link rel="stylesheet" href="/static/AuthTOTP.38d394c2.css">`,
} }

View file

@ -63,4 +63,11 @@ footer {
.pure-button-xlarge { .pure-button-xlarge {
font-size: 125%; font-size: 125%;
} }
.error-message {
background-color: pink;
border: 1px solid red;
padding: 2px;
color: red;
}

View file

@ -1,34 +1,32 @@
{{ template "_base.html" . }} {{ template "_base.html" . }}
{{ define "title" }}{{ block "form/title" . }}Form{{ end }}{{ end }} {{ define "title" }}{{ block "form/title" . }}Form{{ end }}{{ end }}
{{ define "header/time" }} {{ define "header" }}<!-- no header -->{{ end }}
<!-- no header/time --> {{ define "header/time" }}<!-- no time -->{{ end }}
{{ end }}
{{ define "header"}}
<!-- no header -->
{{ end }}
{{ define "content" }} {{ define "content" }}
<div class="pure-u-1"> <div class="pure-u-1">
{{ block "form/message" . }}
{{ $E := .Error }}
{{ if not (eq $E "") }}
<div>
{{ $E }}
</div>
{{ end }}
{{ end }}
{{ block "form/extra" . }}<!-- no extra -->{{ end }} {{ block "form/extra" . }}<!-- no extra -->{{ end }}
<form class="pure-form pure-form-aligned" method="POST"> <form class="pure-form pure-form-aligned" method="POST">
<fieldset> <fieldset>
<legend>{{ template "form/title" . }}</legend> <legend>{{ template "form/title" . }}</legend>
{{ block "form/message" . }}
{{ $E := .Error }}
{{ if not (eq $E "") }}
<div class="pure-form-group">
<p class="error-message">
{{ $E }}
</p>
</div>
{{ end }}
{{ end }}
{{ block "form/inside" . }}<!-- no inside -->{{ end }} {{ block "form/inside" . }}<!-- no inside -->{{ end }}
{{ .Form }} {{ .Form }}
<input type="submit" value="{{ block "form/button" .}}Submit{{ end }}" class="pure-button"> <input type="submit" value="{{ block " form/button" .}}Submit{{ end }}" class="pure-button">
</fieldset> </fieldset>
</form> </form>
</div> </div>
{{ end }} {{ end }}

View file

@ -44,9 +44,13 @@ type Form[D any] struct {
RenderForm func(context FormContext, w http.ResponseWriter, r *http.Request) RenderForm func(context FormContext, w http.ResponseWriter, r *http.Request)
// RenderTemplate represents an optional form to display to the user when RenderForm is nil // RenderTemplate represents an optional form to display to the user when RenderForm is nil
// It is passed a [FormContext] instance. // It is passed the return value of [RenderTemplateContext], or a [FormContext] instance if this does not exist.
RenderTemplate *template.Template RenderTemplate *template.Template
// RenderTemplateContext is the context to be used for RenderTemplate.
// When nil, assumed to be the identify function
RenderTemplateContext func(ctx FormContext, r *http.Request) any
// Validate, if non-nil, validates the given submitted values. // Validate, if non-nil, validates the given submitted values.
// There is no guarantee that the values are set. // There is no guarantee that the values are set.
Validate func(r *http.Request, values map[string]string) (D, error) Validate func(r *http.Request, values map[string]string) (D, error)
@ -151,8 +155,16 @@ func (form *Form[D]) renderForm(err error, values map[string]string, w http.Resp
panic("form.RenderForm and form.Form are nil") panic("form.RenderForm and form.Form are nil")
} }
// get the template context
var tplctx any
if form.RenderTemplateContext == nil {
tplctx = ctx
} else {
tplctx = form.RenderTemplateContext(ctx, r)
}
// render the form // render the form
WriteHTML(ctx, nil, form.RenderTemplate, "", w, r) WriteHTML(tplctx, nil, form.RenderTemplate, "", w, r)
} }
// FormContext is passed to Form.Form when used // FormContext is passed to Form.Form when used