Better semantics, esp. for register page
This commit is contained in:
parent
d9dc6caea3
commit
8c4334e063
@ -1,6 +1,6 @@
|
||||
{
|
||||
"Method": "POST",
|
||||
"Form": {
|
||||
"Elements": {
|
||||
"Username": {
|
||||
"Label": "Username",
|
||||
"Name": "username",
|
||||
|
||||
@ -4,12 +4,12 @@ import (
|
||||
"encoding/xml"
|
||||
)
|
||||
|
||||
type Form struct {
|
||||
type Form[T any] struct {
|
||||
Method string
|
||||
Content any `json:"Form"`
|
||||
Elements T
|
||||
}
|
||||
|
||||
func (i Form) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
||||
func (i Form[T]) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
||||
start.Name = xml.Name{Local: "Form"}
|
||||
|
||||
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Method"}, Value: i.Method})
|
||||
@ -19,7 +19,7 @@ func (i Form) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
||||
return err
|
||||
}
|
||||
|
||||
err = e.Encode(i.Content)
|
||||
err = e.Encode(i.Elements)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -6,7 +6,6 @@ import (
|
||||
)
|
||||
|
||||
type Input struct {
|
||||
// TODO: Perhaps inputs should be allowed to have children.
|
||||
Label string
|
||||
Type string `json:",omitempty"`
|
||||
Name string
|
||||
|
||||
@ -15,9 +15,9 @@ type content struct {
|
||||
}
|
||||
|
||||
func TestSnapshotForm(t *testing.T) {
|
||||
form := htmlx.Form{
|
||||
form := htmlx.Form[content]{
|
||||
Method: "POST",
|
||||
Content: content{
|
||||
Elements: content{
|
||||
Username: htmlx.Input{
|
||||
Label: "Username",
|
||||
Name: "username",
|
||||
|
||||
@ -21,7 +21,7 @@ func (i Submit) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
||||
}
|
||||
|
||||
if err := e.EncodeToken(start); err != nil {
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
if err := e.EncodeToken(xml.CharData(i.Label)); err != nil {
|
||||
|
||||
@ -1,19 +1,26 @@
|
||||
package props
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"lishwist/http/props/htmlx"
|
||||
"lishwist/http/templates"
|
||||
)
|
||||
|
||||
type Register struct {
|
||||
Navbar templates.NavCollapse
|
||||
GeneralError string `json:",omitempty"`
|
||||
type RegisterForm struct {
|
||||
Username htmlx.Input
|
||||
Password htmlx.Input
|
||||
ConfirmPassword htmlx.Input
|
||||
Submit htmlx.Submit
|
||||
}
|
||||
|
||||
func (p *Register) Validate() (valid bool) {
|
||||
type Register struct {
|
||||
Nav templates.NavCollapse
|
||||
Error string `json:",omitempty" xml:",omitempty"`
|
||||
Warning template.HTML `json:",omitempty" xml:",omitempty"`
|
||||
Form htmlx.Form[RegisterForm]
|
||||
}
|
||||
|
||||
func (p *RegisterForm) Validate() (valid bool) {
|
||||
valid = true
|
||||
|
||||
if p.Password.Value != p.ConfirmPassword.Value {
|
||||
@ -38,15 +45,21 @@ func (p *Register) Validate() (valid bool) {
|
||||
|
||||
func NewRegister(usernameVal, passwordVal, confirmPassVal string) *Register {
|
||||
return &Register{
|
||||
Navbar: templates.DefaultNavCollapse(),
|
||||
GeneralError: "",
|
||||
Nav: templates.DefaultNavCollapse(),
|
||||
Error: "",
|
||||
Warning: template.HTML(`<p>Your password will be stored in a safe, responsible manner; but don't trust my programming skills!</p><p class="mb-0">Maybe use a password here that you don't use for important things...</p>`),
|
||||
Form: htmlx.Form[RegisterForm]{
|
||||
Method: "POST",
|
||||
Elements: RegisterForm{
|
||||
Username: htmlx.Input{
|
||||
Label: "Username",
|
||||
Name: "username",
|
||||
Required: true,
|
||||
MinLength: 4,
|
||||
Value: usernameVal,
|
||||
},
|
||||
Password: htmlx.Input{
|
||||
Label: "Password",
|
||||
Type: "password",
|
||||
Name: "newPassword",
|
||||
Required: true,
|
||||
@ -54,10 +67,16 @@ func NewRegister(usernameVal, passwordVal, confirmPassVal string) *Register {
|
||||
Value: passwordVal,
|
||||
},
|
||||
ConfirmPassword: htmlx.Input{
|
||||
Label: "Confirm password",
|
||||
Type: "password",
|
||||
Name: "confirmPassword",
|
||||
Required: true,
|
||||
Value: confirmPassVal,
|
||||
},
|
||||
Submit: htmlx.Submit{
|
||||
Label: "Register",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,7 +30,7 @@ func Login(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
|
||||
login.SuccessfulSetPassword = flashProps.SuccessfulSetPassword
|
||||
}
|
||||
|
||||
body := htmlx.Form{Method: "POST", Content: login}
|
||||
body := htmlx.Form[*props.Login]{Method: "POST", Elements: login}
|
||||
return rsvp.Response{TemplateName: "login.gotmpl", Body: body}
|
||||
}
|
||||
|
||||
@ -55,7 +55,7 @@ func LoginPost(session *response.Session, h http.Header, r *http.Request) rsvp.R
|
||||
if !valid {
|
||||
session.FlashSet(&login)
|
||||
log.Printf("Invalid login submission: %#v\n", login)
|
||||
resp.Body = htmlx.Form{Method: "POST", Content: login}
|
||||
resp.Body = htmlx.Form[*props.Login]{Method: "POST", Elements: login}
|
||||
return resp
|
||||
}
|
||||
|
||||
@ -67,13 +67,13 @@ func LoginPost(session *response.Session, h http.Header, r *http.Request) rsvp.R
|
||||
login.GeneralError = "Username or password invalid. If you're having trouble accessing your account, you may want to consider asking the System Admin (Thomas) to reset your password"
|
||||
session.FlashSet(&login)
|
||||
log.Printf("Invalid credentials: %s: %#v\n", err, login)
|
||||
resp.Body = htmlx.Form{Method: "POST", Content: login}
|
||||
resp.Body = htmlx.Form[*props.Login]{Method: "POST", Elements: login}
|
||||
return resp
|
||||
default:
|
||||
login.GeneralError = "Something went wrong."
|
||||
session.FlashSet(&login)
|
||||
log.Printf("Login error: %s\n", err)
|
||||
resp.Body = htmlx.Form{Method: "POST", Content: login}
|
||||
resp.Body = htmlx.Form[*props.Login]{Method: "POST", Elements: login}
|
||||
return resp
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,11 +18,11 @@ func Register(session *response.Session, h http.Header, r *http.Request) rsvp.Re
|
||||
|
||||
flashProps, _ := flash.(*props.Register)
|
||||
if flashProps != nil {
|
||||
register.Username.Value = flashProps.Username.Value
|
||||
register.Form.Elements.Username.Value = flashProps.Form.Elements.Username.Value
|
||||
|
||||
register.GeneralError = flashProps.GeneralError
|
||||
register.Username.Error = flashProps.Username.Error
|
||||
register.ConfirmPassword.Error = flashProps.ConfirmPassword.Error
|
||||
register.Error = flashProps.Error
|
||||
register.Form.Elements.Username.Error = flashProps.Form.Elements.Username.Error
|
||||
register.Form.Elements.ConfirmPassword.Error = flashProps.Form.Elements.ConfirmPassword.Error
|
||||
}
|
||||
|
||||
return response.Data("register.gotmpl", register)
|
||||
@ -46,9 +46,9 @@ func RegisterPost(s *response.Session, h http.Header, r *http.Request) rsvp.Resp
|
||||
|
||||
register := props.NewRegister(username, newPassword, confirmPassword)
|
||||
|
||||
valid := register.Validate()
|
||||
register.Password.Value = ""
|
||||
register.ConfirmPassword.Value = ""
|
||||
valid := register.Form.Elements.Validate()
|
||||
register.Form.Elements.Password.Value = ""
|
||||
register.Form.Elements.ConfirmPassword.Value = ""
|
||||
if !valid {
|
||||
s.FlashSet(®ister)
|
||||
log.Printf("Invalid register submission: %#v\n", register)
|
||||
@ -58,9 +58,9 @@ func RegisterPost(s *response.Session, h http.Header, r *http.Request) rsvp.Resp
|
||||
_, err := lishwist.Register(username, newPassword)
|
||||
if err != nil {
|
||||
if errors.Is(err, lishwist.ErrorUsernameTaken) {
|
||||
register.Username.Error = "Username is taken"
|
||||
register.Form.Elements.Username.Error = "Username is taken"
|
||||
} else {
|
||||
register.GeneralError = "Something went wrong."
|
||||
register.Error = "Something went wrong."
|
||||
}
|
||||
s.FlashSet(®ister)
|
||||
log.Printf("Registration failed: %s\n", err)
|
||||
|
||||
@ -25,7 +25,6 @@
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
|
||||
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
|
||||
crossorigin="anonymous"></script>
|
||||
|
||||
<script>
|
||||
function getSubmissionNames(form) {
|
||||
return Array.from(new FormData(form).keys());
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
{{template "head"}}
|
||||
</head>
|
||||
|
||||
{{with .Content}}
|
||||
{{with .Elements}}
|
||||
<body>
|
||||
<div style="height: 100svh;" class="d-flex flex-column">
|
||||
{{template "navbar"}}
|
||||
@ -37,7 +37,7 @@
|
||||
<div class="mb-3">
|
||||
{{template "hyperlink" .RegisterLink}}
|
||||
</div>
|
||||
<input class="btn btn-primary" type="submit" value="{{.Submit.Value}}">
|
||||
<input class="btn btn-primary" type="submit" value="{{.Submit.Label}}">
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@ -6,34 +6,39 @@
|
||||
|
||||
<body>
|
||||
<div style="height: 100svh;" class="d-flex flex-column">
|
||||
{{template "navbar" .Navbar}}
|
||||
{{template "navbar" .Nav}}
|
||||
<div class="container d-flex flex-grow-1 justify-content-center align-items-center flex-column">
|
||||
{{with .Warning}}
|
||||
<div class="alert alert-warning" role="alert">
|
||||
<p>Your password will be stored in a safe, responsible manner; but don't trust my programming skills!</p>
|
||||
<p class="mb-0">Maybe use a password here that you don't use for important things...</p>
|
||||
{{.}}
|
||||
</div>
|
||||
{{with .GeneralError}}
|
||||
{{end}}
|
||||
{{with .Error}}
|
||||
<div class="alert alert-danger" role="alert">
|
||||
<p class="mb-0">{{.}}</p>
|
||||
</div>
|
||||
{{end}}
|
||||
<form method="post">
|
||||
{{with .Form}}
|
||||
<form method="{{.Method}}">
|
||||
{{with .Elements}}
|
||||
<div class="d-flex flex-column">
|
||||
<label>
|
||||
Username
|
||||
{{.Username.Label}}
|
||||
{{template "input" .Username}}
|
||||
</label>
|
||||
<label>
|
||||
Password
|
||||
{{.Password.Label}}
|
||||
{{template "input" .Password}}
|
||||
</label>
|
||||
<label>
|
||||
Confirm password
|
||||
{{.ConfirmPassword.Label}}
|
||||
{{template "input" .ConfirmPassword}}
|
||||
</label>
|
||||
<input class="btn btn-primary" type="submit" value="Register">
|
||||
<input class="btn btn-primary" type="submit" value="{{.Submit.Label}}">
|
||||
</div>
|
||||
{{end}}
|
||||
</form>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
@ -20,8 +20,8 @@ type User struct {
|
||||
}
|
||||
|
||||
type NavCollapse struct {
|
||||
User *User
|
||||
AccountLink *AccountLink
|
||||
User *User `json:",omitempty"`
|
||||
AccountLink *AccountLink `json:",omitempty"`
|
||||
Links []htmlx.Link
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user