feat: better xml semantics for login page
This commit is contained in:
parent
86c1e4fea3
commit
44d920d336
5
assert/go.mod
Normal file
5
assert/go.mod
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module lishwist/assert
|
||||||
|
|
||||||
|
go 1.23.0
|
||||||
|
|
||||||
|
toolchain go1.23.3
|
||||||
@ -2,9 +2,10 @@ package assert
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"encoding/xml"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -13,6 +14,13 @@ func pathExists(path string) bool {
|
|||||||
return !errors.Is(err, os.ErrNotExist)
|
return !errors.Is(err, os.ErrNotExist)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ensureSuffix(s, suffix string) string {
|
||||||
|
if !strings.HasSuffix(s, suffix) {
|
||||||
|
s = s + suffix
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
func writeSnapshot(t *testing.T, path, actual string) {
|
func writeSnapshot(t *testing.T, path, actual string) {
|
||||||
f, err := os.Create(path)
|
f, err := os.Create(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -35,14 +43,18 @@ func TextSnapshot(t *testing.T, path, actual string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
actual = ensureSuffix(actual, "\n")
|
||||||
|
|
||||||
content, err := os.ReadFile(path)
|
content, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to read snapshot file: %s", err)
|
t.Fatalf("Failed to read snapshot file: %s", err)
|
||||||
}
|
}
|
||||||
expected := string(content)
|
expected := string(content)
|
||||||
|
|
||||||
|
expected = ensureSuffix(expected, "\n")
|
||||||
|
|
||||||
if expected != actual {
|
if expected != actual {
|
||||||
t.Errorf("Value doesn't match snapshot %s:\n%s", path, actual)
|
t.Errorf("Value doesn't match snapshot %s:\n\nExpected:\n%s\n\nActual:\n%s\n", path, expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
if os.Getenv("UPDATE_SNAPSHOTS") == "" {
|
if os.Getenv("UPDATE_SNAPSHOTS") == "" {
|
||||||
@ -50,7 +62,7 @@ func TextSnapshot(t *testing.T, path, actual string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
writeSnapshot(t, path, actual)
|
writeSnapshot(t, path, actual)
|
||||||
fmt.Printf("Snapshot file %s updated\n", path)
|
t.Logf("Snapshot file %s updated\n", path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func JsonSnapshot(t *testing.T, path string, actual any) {
|
func JsonSnapshot(t *testing.T, path string, actual any) {
|
||||||
@ -60,3 +72,11 @@ func JsonSnapshot(t *testing.T, path string, actual any) {
|
|||||||
}
|
}
|
||||||
TextSnapshot(t, path, string(data))
|
TextSnapshot(t, path, string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func XmlSnapshot(t *testing.T, path string, actual any) {
|
||||||
|
data, err := xml.MarshalIndent(actual, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Snapshot failed to serialize actual to XML: %s", err)
|
||||||
|
}
|
||||||
|
TextSnapshot(t, path, string(data))
|
||||||
|
}
|
||||||
@ -3,7 +3,7 @@ package lishwist_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"lishwist/core/internal/assert"
|
"lishwist/assert"
|
||||||
"lishwist/core/internal/fixtures"
|
"lishwist/core/internal/fixtures"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
1
go.work
1
go.work
@ -1,6 +1,7 @@
|
|||||||
go 1.24.5
|
go 1.24.5
|
||||||
|
|
||||||
use (
|
use (
|
||||||
|
./assert
|
||||||
./core
|
./core
|
||||||
./http
|
./http
|
||||||
)
|
)
|
||||||
|
|||||||
@ -1,43 +0,0 @@
|
|||||||
package api
|
|
||||||
|
|
||||||
import (
|
|
||||||
"lishwist/http/templates"
|
|
||||||
)
|
|
||||||
|
|
||||||
type LoginProps struct {
|
|
||||||
GeneralError string `json:",omitempty"`
|
|
||||||
SuccessfulRegistration bool `json:",omitempty"`
|
|
||||||
SuccessfulSetPassword bool `json:",omitempty"`
|
|
||||||
Username templates.InputProps
|
|
||||||
Password templates.InputProps
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewLoginProps(username, password string) *LoginProps {
|
|
||||||
return &LoginProps{
|
|
||||||
Username: templates.InputProps{
|
|
||||||
Name: "username",
|
|
||||||
Required: true,
|
|
||||||
Value: username,
|
|
||||||
},
|
|
||||||
Password: templates.InputProps{
|
|
||||||
Name: "password",
|
|
||||||
Type: "password",
|
|
||||||
Required: true,
|
|
||||||
Value: password,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *LoginProps) Validate() (valid bool) {
|
|
||||||
valid = true
|
|
||||||
|
|
||||||
if !p.Username.Validate() {
|
|
||||||
valid = false
|
|
||||||
}
|
|
||||||
|
|
||||||
if !p.Password.Validate() {
|
|
||||||
valid = false
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
@ -1,97 +0,0 @@
|
|||||||
package api
|
|
||||||
|
|
||||||
import (
|
|
||||||
"lishwist/http/templates"
|
|
||||||
)
|
|
||||||
|
|
||||||
type RegisterProps struct {
|
|
||||||
Navbar templates.NavCollapse
|
|
||||||
GeneralError string `json:",omitempty"`
|
|
||||||
Username templates.InputProps
|
|
||||||
Password templates.InputProps
|
|
||||||
ConfirmPassword templates.InputProps
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *RegisterProps) Validate() (valid bool) {
|
|
||||||
valid = true
|
|
||||||
|
|
||||||
if p.Password.Value != p.ConfirmPassword.Value {
|
|
||||||
p.ConfirmPassword.Error = "Passwords didn't match"
|
|
||||||
valid = false
|
|
||||||
}
|
|
||||||
|
|
||||||
if !p.Username.Validate() {
|
|
||||||
valid = false
|
|
||||||
}
|
|
||||||
|
|
||||||
if !p.Password.Validate() {
|
|
||||||
valid = false
|
|
||||||
}
|
|
||||||
|
|
||||||
if !p.ConfirmPassword.Validate() {
|
|
||||||
valid = false
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRegisterProps(usernameVal, passwordVal, confirmPassVal string) *RegisterProps {
|
|
||||||
return &RegisterProps{
|
|
||||||
Navbar: templates.DefaultNavCollapse(),
|
|
||||||
GeneralError: "",
|
|
||||||
Username: templates.InputProps{
|
|
||||||
Name: "username",
|
|
||||||
Required: true,
|
|
||||||
MinLength: 4,
|
|
||||||
Value: usernameVal,
|
|
||||||
},
|
|
||||||
Password: templates.InputProps{
|
|
||||||
Type: "password",
|
|
||||||
Name: "newPassword",
|
|
||||||
Required: true,
|
|
||||||
MinLength: 5,
|
|
||||||
Value: passwordVal,
|
|
||||||
},
|
|
||||||
ConfirmPassword: templates.InputProps{
|
|
||||||
Type: "password",
|
|
||||||
Name: "confirmPassword",
|
|
||||||
Required: true,
|
|
||||||
Value: confirmPassVal,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// func Register(username, newPassword, confirmPassword string) *RegisterProps {
|
|
||||||
// props := NewRegisterProps(username, newPassword, confirmPassword)
|
|
||||||
|
|
||||||
// valid := props.Validate()
|
|
||||||
// props.Password.Value = ""
|
|
||||||
// props.ConfirmPassword.Value = ""
|
|
||||||
// if !valid {
|
|
||||||
// log.Printf("Invalid props: %#v\n", props)
|
|
||||||
// return props
|
|
||||||
// }
|
|
||||||
|
|
||||||
// existingUser, _ := db.GetUserByName(username)
|
|
||||||
// if existingUser != nil {
|
|
||||||
// log.Printf("Username is taken: %q\n", existingUser.NormalName)
|
|
||||||
// props.Username.Error = "Username is taken"
|
|
||||||
// return props
|
|
||||||
// }
|
|
||||||
|
|
||||||
// hashedPasswordBytes, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.MinCost)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Printf("Failed to hash password: %s\n", err)
|
|
||||||
// props.GeneralError = "Something went wrong. Error code: Aang"
|
|
||||||
// return props
|
|
||||||
// }
|
|
||||||
|
|
||||||
// _, err = db.CreateUser(username, hashedPasswordBytes)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Printf("Failed to create user: %s\n", err)
|
|
||||||
// props.GeneralError = "Something went wrong. Error code: Ozai"
|
|
||||||
// return props
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
3
http/props/htmlx/TestFormXmlRender.snap.xml
Normal file
3
http/props/htmlx/TestFormXmlRender.snap.xml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<Form Method="POST">
|
||||||
|
<Input Label="Message" Name="message" Value=""></Input>
|
||||||
|
</Form>
|
||||||
28
http/props/htmlx/form.go
Normal file
28
http/props/htmlx/form.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package htmlx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Form struct {
|
||||||
|
Method string
|
||||||
|
Content any `json:"Form"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i Form) 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})
|
||||||
|
|
||||||
|
err := e.EncodeToken(start)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = e.Encode(i.Content)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return e.EncodeToken(start.End())
|
||||||
|
}
|
||||||
17
http/props/htmlx/form_test.go
Normal file
17
http/props/htmlx/form_test.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package htmlx_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"lishwist/assert"
|
||||||
|
"lishwist/http/props/htmlx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFormXmlRender(t *testing.T) {
|
||||||
|
form := htmlx.Form{
|
||||||
|
Method: "POST",
|
||||||
|
Content: htmlx.Input{Label: "Message", Name: "message"},
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.XmlSnapshot(t, "TestFormXmlRender.snap.xml", form)
|
||||||
|
}
|
||||||
66
http/props/htmlx/input.go
Normal file
66
http/props/htmlx/input.go
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
package htmlx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Input struct {
|
||||||
|
// TODO: Perhaps inputs should be allowed to have children.
|
||||||
|
Label string
|
||||||
|
Type string `json:",omitempty"`
|
||||||
|
Name string
|
||||||
|
Required bool `json:",omitempty"`
|
||||||
|
Value string
|
||||||
|
MinLength uint `json:",omitempty"`
|
||||||
|
Error string `json:",omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i Input) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
||||||
|
start.Name = xml.Name{Local: "Input"}
|
||||||
|
|
||||||
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Label"}, Value: i.Label})
|
||||||
|
if i.Type != "" {
|
||||||
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Type"}, Value: i.Type})
|
||||||
|
}
|
||||||
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Name"}, Value: i.Name})
|
||||||
|
if i.Required {
|
||||||
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Required"}, Value: "true"})
|
||||||
|
}
|
||||||
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Value"}, Value: i.Value})
|
||||||
|
if i.MinLength > 0 {
|
||||||
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "MinLength"}, Value: fmt.Sprintf("%d", i.MinLength)})
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := e.EncodeToken(start); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if i.Error != "" {
|
||||||
|
errorStart := xml.StartElement{Name: xml.Name{Local: "Error"}}
|
||||||
|
if err := e.EncodeElement(i.Error, errorStart); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := e.EncodeToken(start.End()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Input) Validate() bool {
|
||||||
|
if p.Required && p.Value == "" {
|
||||||
|
p.Error = fmt.Sprintf("%v is required", p.Name)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
value_len := len(p.Value)
|
||||||
|
if p.MinLength > 0 && int(p.MinLength) > value_len {
|
||||||
|
p.Error = fmt.Sprintf("%v requires at least %v characters (currently %v characters)", p.Name, p.MinLength, value_len)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
24
http/props/htmlx/link.go
Normal file
24
http/props/htmlx/link.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package htmlx
|
||||||
|
|
||||||
|
import "encoding/xml"
|
||||||
|
|
||||||
|
type Link struct {
|
||||||
|
Href string
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Link) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
||||||
|
start.Name = xml.Name{Local: "A"}
|
||||||
|
|
||||||
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Href"}, Value: i.Href})
|
||||||
|
|
||||||
|
if err := e.EncodeToken(start); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := e.EncodeToken(xml.CharData(i.Name)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return e.EncodeToken(start.End())
|
||||||
|
}
|
||||||
36
http/props/htmlx/submit.go
Normal file
36
http/props/htmlx/submit.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package htmlx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Submit struct {
|
||||||
|
Label string
|
||||||
|
Name string `json:",omitempty"`
|
||||||
|
Value string `json:",omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i Submit) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
||||||
|
start.Name = xml.Name{Local: "Submit"}
|
||||||
|
|
||||||
|
if i.Name != "" {
|
||||||
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Name"}, Value: i.Name})
|
||||||
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Value"}, Value: i.Value})
|
||||||
|
} else if i.Value != "" {
|
||||||
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Value"}, Value: i.Value})
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := e.EncodeToken(start); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := e.EncodeToken(xml.CharData(i.Label)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := e.EncodeToken(start.End()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
54
http/props/login.go
Normal file
54
http/props/login.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package props
|
||||||
|
|
||||||
|
import (
|
||||||
|
"lishwist/http/props/htmlx"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Login struct {
|
||||||
|
GeneralError string `json:",omitempty" xml:",omitempty"`
|
||||||
|
SuccessfulRegistration bool `json:",omitempty" xml:",omitempty"`
|
||||||
|
SuccessfulSetPassword bool `json:",omitempty" xml:",omitempty"`
|
||||||
|
Username htmlx.Input
|
||||||
|
Password htmlx.Input
|
||||||
|
RegisterLink htmlx.Link
|
||||||
|
Submit htmlx.Submit
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLogin(username, password string) *Login {
|
||||||
|
return &Login{
|
||||||
|
Username: htmlx.Input{
|
||||||
|
Label: "Username",
|
||||||
|
Name: "username",
|
||||||
|
Required: true,
|
||||||
|
Value: username,
|
||||||
|
},
|
||||||
|
Password: htmlx.Input{
|
||||||
|
Label: "Password",
|
||||||
|
Name: "password",
|
||||||
|
Type: "password",
|
||||||
|
Required: true,
|
||||||
|
Value: password,
|
||||||
|
},
|
||||||
|
RegisterLink: htmlx.Link{
|
||||||
|
Name: "Register",
|
||||||
|
Href: "/register",
|
||||||
|
},
|
||||||
|
Submit: htmlx.Submit{
|
||||||
|
Label: "Login",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Login) Validate() (valid bool) {
|
||||||
|
valid = true
|
||||||
|
|
||||||
|
if !p.Username.Validate() {
|
||||||
|
valid = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if !p.Password.Validate() {
|
||||||
|
valid = false
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
63
http/props/register.go
Normal file
63
http/props/register.go
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
package props
|
||||||
|
|
||||||
|
import (
|
||||||
|
"lishwist/http/props/htmlx"
|
||||||
|
"lishwist/http/templates"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Register struct {
|
||||||
|
Navbar templates.NavCollapse
|
||||||
|
GeneralError string `json:",omitempty"`
|
||||||
|
Username htmlx.Input
|
||||||
|
Password htmlx.Input
|
||||||
|
ConfirmPassword htmlx.Input
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Register) Validate() (valid bool) {
|
||||||
|
valid = true
|
||||||
|
|
||||||
|
if p.Password.Value != p.ConfirmPassword.Value {
|
||||||
|
p.ConfirmPassword.Error = "Passwords didn't match"
|
||||||
|
valid = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if !p.Username.Validate() {
|
||||||
|
valid = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if !p.Password.Validate() {
|
||||||
|
valid = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if !p.ConfirmPassword.Validate() {
|
||||||
|
valid = false
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRegister(usernameVal, passwordVal, confirmPassVal string) *Register {
|
||||||
|
return &Register{
|
||||||
|
Navbar: templates.DefaultNavCollapse(),
|
||||||
|
GeneralError: "",
|
||||||
|
Username: htmlx.Input{
|
||||||
|
Name: "username",
|
||||||
|
Required: true,
|
||||||
|
MinLength: 4,
|
||||||
|
Value: usernameVal,
|
||||||
|
},
|
||||||
|
Password: htmlx.Input{
|
||||||
|
Type: "password",
|
||||||
|
Name: "newPassword",
|
||||||
|
Required: true,
|
||||||
|
MinLength: 5,
|
||||||
|
Value: passwordVal,
|
||||||
|
},
|
||||||
|
ConfirmPassword: htmlx.Input{
|
||||||
|
Type: "password",
|
||||||
|
Name: "confirmPassword",
|
||||||
|
Required: true,
|
||||||
|
Value: confirmPassVal,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -5,7 +5,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
lishwist "lishwist/core"
|
lishwist "lishwist/core"
|
||||||
"lishwist/http/api"
|
"lishwist/http/props"
|
||||||
|
"lishwist/http/props/htmlx"
|
||||||
"lishwist/http/response"
|
"lishwist/http/response"
|
||||||
"lishwist/http/templates"
|
"lishwist/http/templates"
|
||||||
|
|
||||||
@ -16,8 +17,8 @@ type AccountProps struct {
|
|||||||
Navbar templates.NavCollapse
|
Navbar templates.NavCollapse
|
||||||
GeneralError string `json:",omitempty"`
|
GeneralError string `json:",omitempty"`
|
||||||
PasswordFromAdmin bool `json:",omitempty"`
|
PasswordFromAdmin bool `json:",omitempty"`
|
||||||
Password templates.InputProps
|
Password htmlx.Input
|
||||||
ConfirmPassword templates.InputProps
|
ConfirmPassword htmlx.Input
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *AccountProps) Validate() (valid bool) {
|
func (p *AccountProps) Validate() (valid bool) {
|
||||||
@ -42,18 +43,18 @@ func (p *AccountProps) Validate() (valid bool) {
|
|||||||
func NewAccountProps(username string, passwordFromAdmin bool, passwordVal, confirmPassVal string) *AccountProps {
|
func NewAccountProps(username string, passwordFromAdmin bool, passwordVal, confirmPassVal string) *AccountProps {
|
||||||
return &AccountProps{
|
return &AccountProps{
|
||||||
Navbar: templates.NavCollapse{
|
Navbar: templates.NavCollapse{
|
||||||
Links: []templates.Link{{Href: "/", Name: "Home"}},
|
Links: []htmlx.Link{{Href: "/", Name: "Home"}},
|
||||||
User: &templates.User{Name: username},
|
User: &templates.User{Name: username},
|
||||||
},
|
},
|
||||||
PasswordFromAdmin: passwordFromAdmin,
|
PasswordFromAdmin: passwordFromAdmin,
|
||||||
Password: templates.InputProps{
|
Password: htmlx.Input{
|
||||||
Type: "password",
|
Type: "password",
|
||||||
Name: "new_password",
|
Name: "new_password",
|
||||||
Required: true,
|
Required: true,
|
||||||
MinLength: 5,
|
MinLength: 5,
|
||||||
Value: passwordVal,
|
Value: passwordVal,
|
||||||
},
|
},
|
||||||
ConfirmPassword: templates.InputProps{
|
ConfirmPassword: htmlx.Input{
|
||||||
Type: "password",
|
Type: "password",
|
||||||
Name: "confirm_password",
|
Name: "confirm_password",
|
||||||
Required: true,
|
Required: true,
|
||||||
@ -93,27 +94,27 @@ func AccountPost(app *lishwist.Session, session *response.Session, h http.Header
|
|||||||
newPassword := r.Form.Get("new_password")
|
newPassword := r.Form.Get("new_password")
|
||||||
confirmPassword := r.Form.Get("confirm_password")
|
confirmPassword := r.Form.Get("confirm_password")
|
||||||
|
|
||||||
props := NewAccountProps(user.Name, user.PasswordFromAdmin, newPassword, confirmPassword)
|
account := NewAccountProps(user.Name, user.PasswordFromAdmin, newPassword, confirmPassword)
|
||||||
|
|
||||||
valid := props.Validate()
|
valid := account.Validate()
|
||||||
props.Password.Value = ""
|
account.Password.Value = ""
|
||||||
props.ConfirmPassword.Value = ""
|
account.ConfirmPassword.Value = ""
|
||||||
if !valid {
|
if !valid {
|
||||||
log.Printf("Invalid account props: %#v\n", props)
|
log.Printf("Invalid account submission: %#v\n", account)
|
||||||
session.FlashSet(&props)
|
session.FlashSet(&account)
|
||||||
return rsvp.SeeOther("/account", props)
|
return rsvp.SeeOther("/account", account)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = user.SetPassword(newPassword)
|
err = user.SetPassword(newPassword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
props.GeneralError = "Something went wrong."
|
account.GeneralError = "Something went wrong."
|
||||||
log.Printf("Set password failed: %s\n", err)
|
log.Printf("Set password failed: %s\n", err)
|
||||||
session.FlashSet(&props)
|
session.FlashSet(&account)
|
||||||
return rsvp.SeeOther("/account", props)
|
return rsvp.SeeOther("/account", account)
|
||||||
}
|
}
|
||||||
|
|
||||||
session.RemoveValue("sessionKey")
|
session.RemoveValue("sessionKey")
|
||||||
|
|
||||||
session.FlashSet(&api.LoginProps{SuccessfulSetPassword: true})
|
session.FlashSet(&props.Login{SuccessfulSetPassword: true})
|
||||||
return rsvp.SeeOther("/", "Set password successful!")
|
return rsvp.SeeOther("/", "Set password successful!")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,29 +7,31 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
lishwist "lishwist/core"
|
lishwist "lishwist/core"
|
||||||
"lishwist/http/api"
|
"lishwist/http/props"
|
||||||
|
"lishwist/http/props/htmlx"
|
||||||
"lishwist/http/response"
|
"lishwist/http/response"
|
||||||
|
|
||||||
"github.com/Teajey/rsvp"
|
"github.com/Teajey/rsvp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Login(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
|
func Login(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
|
||||||
props := api.NewLoginProps("", "")
|
login := props.NewLogin("", "")
|
||||||
|
|
||||||
flash := s.FlashGet()
|
flash := s.FlashGet()
|
||||||
flashProps, ok := flash.(*api.LoginProps)
|
flashProps, ok := flash.(*props.Login)
|
||||||
if ok {
|
if ok {
|
||||||
props.Username.Value = flashProps.Username.Value
|
login.Username.Value = flashProps.Username.Value
|
||||||
|
|
||||||
props.GeneralError = flashProps.GeneralError
|
login.GeneralError = flashProps.GeneralError
|
||||||
props.Username.Error = flashProps.Username.Error
|
login.Username.Error = flashProps.Username.Error
|
||||||
props.Password.Error = flashProps.Password.Error
|
login.Password.Error = flashProps.Password.Error
|
||||||
|
|
||||||
props.SuccessfulRegistration = flashProps.SuccessfulRegistration
|
login.SuccessfulRegistration = flashProps.SuccessfulRegistration
|
||||||
props.SuccessfulSetPassword = flashProps.SuccessfulSetPassword
|
login.SuccessfulSetPassword = flashProps.SuccessfulSetPassword
|
||||||
}
|
}
|
||||||
|
|
||||||
return rsvp.Response{TemplateName: "login.gotmpl", Body: props}
|
body := htmlx.Form{Method: "POST", Content: login}
|
||||||
|
return rsvp.Response{TemplateName: "login.gotmpl", Body: body}
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoginPost(session *response.Session, h http.Header, r *http.Request) rsvp.Response {
|
func LoginPost(session *response.Session, h http.Header, r *http.Request) rsvp.Response {
|
||||||
@ -44,15 +46,16 @@ func LoginPost(session *response.Session, h http.Header, r *http.Request) rsvp.R
|
|||||||
password = r.Form.Get("password")
|
password = r.Form.Get("password")
|
||||||
}
|
}
|
||||||
|
|
||||||
props := api.NewLoginProps(username, password)
|
login := props.NewLogin(username, password)
|
||||||
|
|
||||||
resp := rsvp.SeeOther(r.URL.Path, props)
|
resp := rsvp.SeeOther(r.URL.Path, login)
|
||||||
|
|
||||||
valid := props.Validate()
|
valid := login.Validate()
|
||||||
props.Password.Value = ""
|
login.Password.Value = ""
|
||||||
if !valid {
|
if !valid {
|
||||||
session.FlashSet(&props)
|
session.FlashSet(&login)
|
||||||
log.Printf("Invalid props: %#v\n", props)
|
log.Printf("Invalid login submission: %#v\n", login)
|
||||||
|
resp.Body = htmlx.Form{Method: "POST", Content: login}
|
||||||
return resp
|
return resp
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,14 +64,16 @@ func LoginPost(session *response.Session, h http.Header, r *http.Request) rsvp.R
|
|||||||
var targ lishwist.ErrorInvalidCredentials
|
var targ lishwist.ErrorInvalidCredentials
|
||||||
switch {
|
switch {
|
||||||
case errors.As(err, &targ):
|
case errors.As(err, &targ):
|
||||||
props.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"
|
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(&props)
|
session.FlashSet(&login)
|
||||||
log.Printf("Invalid credentials: %s: %#v\n", err, props)
|
log.Printf("Invalid credentials: %s: %#v\n", err, login)
|
||||||
|
resp.Body = htmlx.Form{Method: "POST", Content: login}
|
||||||
return resp
|
return resp
|
||||||
default:
|
default:
|
||||||
props.GeneralError = "Something went wrong."
|
login.GeneralError = "Something went wrong."
|
||||||
session.FlashSet(&props)
|
session.FlashSet(&login)
|
||||||
log.Printf("Login error: %s\n", err)
|
log.Printf("Login error: %s\n", err)
|
||||||
|
resp.Body = htmlx.Form{Method: "POST", Content: login}
|
||||||
return resp
|
return resp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,7 @@ package routing
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
lishwist "lishwist/core"
|
lishwist "lishwist/core"
|
||||||
"lishwist/http/api"
|
"lishwist/http/props"
|
||||||
"lishwist/http/response"
|
"lishwist/http/response"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -12,20 +12,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Register(session *response.Session, h http.Header, r *http.Request) rsvp.Response {
|
func Register(session *response.Session, h http.Header, r *http.Request) rsvp.Response {
|
||||||
props := api.NewRegisterProps("", "", "")
|
register := props.NewRegister("", "", "")
|
||||||
|
|
||||||
flash := session.FlashGet()
|
flash := session.FlashGet()
|
||||||
|
|
||||||
flashProps, _ := flash.(*api.RegisterProps)
|
flashProps, _ := flash.(*props.Register)
|
||||||
if flashProps != nil {
|
if flashProps != nil {
|
||||||
props.Username.Value = flashProps.Username.Value
|
register.Username.Value = flashProps.Username.Value
|
||||||
|
|
||||||
props.GeneralError = flashProps.GeneralError
|
register.GeneralError = flashProps.GeneralError
|
||||||
props.Username.Error = flashProps.Username.Error
|
register.Username.Error = flashProps.Username.Error
|
||||||
props.ConfirmPassword.Error = flashProps.ConfirmPassword.Error
|
register.ConfirmPassword.Error = flashProps.ConfirmPassword.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.Data("register.gotmpl", props)
|
return response.Data("register.gotmpl", register)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterPost(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
|
func RegisterPost(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
|
||||||
@ -44,29 +44,29 @@ func RegisterPost(s *response.Session, h http.Header, r *http.Request) rsvp.Resp
|
|||||||
confirmPassword = r.Form.Get("confirmPassword")
|
confirmPassword = r.Form.Get("confirmPassword")
|
||||||
}
|
}
|
||||||
|
|
||||||
props := api.NewRegisterProps(username, newPassword, confirmPassword)
|
register := props.NewRegister(username, newPassword, confirmPassword)
|
||||||
|
|
||||||
valid := props.Validate()
|
valid := register.Validate()
|
||||||
props.Password.Value = ""
|
register.Password.Value = ""
|
||||||
props.ConfirmPassword.Value = ""
|
register.ConfirmPassword.Value = ""
|
||||||
if !valid {
|
if !valid {
|
||||||
s.FlashSet(&props)
|
s.FlashSet(®ister)
|
||||||
log.Printf("Invalid register props: %#v\n", props)
|
log.Printf("Invalid register submission: %#v\n", register)
|
||||||
return rsvp.SeeOther(r.URL.Path, props)
|
return rsvp.SeeOther(r.URL.Path, register)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := lishwist.Register(username, newPassword)
|
_, err := lishwist.Register(username, newPassword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, lishwist.ErrorUsernameTaken) {
|
if errors.Is(err, lishwist.ErrorUsernameTaken) {
|
||||||
props.Username.Error = "Username is taken"
|
register.Username.Error = "Username is taken"
|
||||||
} else {
|
} else {
|
||||||
props.GeneralError = "Something went wrong."
|
register.GeneralError = "Something went wrong."
|
||||||
}
|
}
|
||||||
s.FlashSet(&props)
|
s.FlashSet(®ister)
|
||||||
log.Printf("Registration failed: %s\n", err)
|
log.Printf("Registration failed: %s\n", err)
|
||||||
return rsvp.SeeOther(r.URL.Path, props)
|
return rsvp.SeeOther(r.URL.Path, register)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.FlashSet(&api.LoginProps{SuccessfulRegistration: true})
|
s.FlashSet(&props.Login{SuccessfulRegistration: true})
|
||||||
return rsvp.SeeOther("/", "Registration successful!")
|
return rsvp.SeeOther("/", "Registration successful!")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,8 +5,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"lishwist/http/api"
|
|
||||||
"lishwist/http/env"
|
"lishwist/http/env"
|
||||||
|
"lishwist/http/props"
|
||||||
"lishwist/http/response"
|
"lishwist/http/response"
|
||||||
"lishwist/http/router"
|
"lishwist/http/router"
|
||||||
"lishwist/http/routing"
|
"lishwist/http/routing"
|
||||||
@ -30,8 +30,8 @@ func prefixPermanentRedirect(before, after string) response.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Create(useSecureCookies bool) *router.VisibilityRouter {
|
func Create(useSecureCookies bool) *router.VisibilityRouter {
|
||||||
gob.Register(&api.RegisterProps{})
|
gob.Register(&props.Register{})
|
||||||
gob.Register(&api.LoginProps{})
|
gob.Register(&props.Login{})
|
||||||
gob.Register(&routing.AccountProps{})
|
gob.Register(&routing.AccountProps{})
|
||||||
|
|
||||||
store := session.NewInMemoryStore([]byte(env.Configuration.SessionSecret))
|
store := session.NewInMemoryStore([]byte(env.Configuration.SessionSecret))
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{{define "input"}}
|
{{define "input"}}
|
||||||
<input style="padding: .375rem .75rem;" class="form-control{{if .Error}} is-invalid{{end}}" {{if .Type}}type="{{.Type}}" {{end}}name="{{.Name}}"
|
<input style="padding: .375rem .75rem;" class="form-control{{if .Error}} is-invalid{{end}}" {{if .Type}}type="{{.Type}}" {{end}}name="{{.Name}}"
|
||||||
value="{{.Value}}" {{if .Required}}required {{end}}{{if .MinLength}}minlength="{{.MinLength}}" {{end}} aria-describedby="{{if .Error}}{{.Name}}Error{{end}}">
|
value="{{.Value}}" {{if .Required}}required {{end}}{{if .MinLength}}minlength="{{.MinLength}}" {{end}}{{with .Error}} aria-describedby="{{.Name}}Error"{{end}}>
|
||||||
{{with .Error}}
|
{{with .Error}}
|
||||||
<div id="{{$.Name}}Error" class="invalid-feedback">
|
<div id="{{$.Name}}Error" class="invalid-feedback">
|
||||||
{{.}}
|
{{.}}
|
||||||
@ -12,6 +12,10 @@
|
|||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
|
{{define "hyperlink"}}
|
||||||
|
<a href="{{.Href}}">{{.Name}}</a>
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{define "head"}}
|
{{define "head"}}
|
||||||
<title>Lishwist</title>
|
<title>Lishwist</title>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
{{template "head"}}
|
{{template "head"}}
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
|
{{with .Content}}
|
||||||
<body>
|
<body>
|
||||||
<div style="height: 100svh;" class="d-flex flex-column">
|
<div style="height: 100svh;" class="d-flex flex-column">
|
||||||
{{template "navbar"}}
|
{{template "navbar"}}
|
||||||
@ -23,23 +24,24 @@
|
|||||||
<p class="mb-0">{{.}}</p>
|
<p class="mb-0">{{.}}</p>
|
||||||
</div>
|
</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
<form method="post">
|
<form method="{{$.Method}}">
|
||||||
<div class="d-flex flex-column">
|
<div class="d-flex flex-column">
|
||||||
<label>
|
<label>
|
||||||
Username
|
{{.Username.Label}}
|
||||||
{{template "input" .Username}}
|
{{template "input" .Username}}
|
||||||
</label>
|
</label>
|
||||||
<label>
|
<label>
|
||||||
Password
|
{{.Password.Label}}
|
||||||
{{template "input" .Password}}
|
{{template "input" .Password}}
|
||||||
</label>
|
</label>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<a href="/register">Register</a>
|
{{template "hyperlink" .RegisterLink}}
|
||||||
</div>
|
</div>
|
||||||
<input class="btn btn-primary" type="submit" value="Login">
|
<input class="btn btn-primary" type="submit" value="{{.Submit.Value}}">
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
|
{{end}}
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@ -1,19 +1,14 @@
|
|||||||
package templates
|
package templates
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"lishwist/http/props/htmlx"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AccountLink struct {
|
type AccountLink struct {
|
||||||
Alert bool
|
Alert bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Link struct {
|
|
||||||
Href string
|
|
||||||
Name string
|
|
||||||
}
|
|
||||||
|
|
||||||
type CopyList struct {
|
type CopyList struct {
|
||||||
Domain string
|
Domain string
|
||||||
Reference string
|
Reference string
|
||||||
@ -27,45 +22,21 @@ type User struct {
|
|||||||
type NavCollapse struct {
|
type NavCollapse struct {
|
||||||
User *User
|
User *User
|
||||||
AccountLink *AccountLink
|
AccountLink *AccountLink
|
||||||
Links []Link
|
Links []htmlx.Link
|
||||||
}
|
}
|
||||||
|
|
||||||
func DefaultNavCollapse() NavCollapse {
|
func DefaultNavCollapse() NavCollapse {
|
||||||
return NavCollapse{Links: []Link{{Href: "/", Name: "Home"}}}
|
return NavCollapse{Links: []htmlx.Link{{Href: "/", Name: "Home"}}}
|
||||||
}
|
}
|
||||||
|
|
||||||
func UserNavCollapse(username string, accountAlert bool) NavCollapse {
|
func UserNavCollapse(username string, accountAlert bool) NavCollapse {
|
||||||
return NavCollapse{
|
return NavCollapse{
|
||||||
Links: []Link{{Href: "/", Name: "Home"}},
|
Links: []htmlx.Link{{Href: "/", Name: "Home"}},
|
||||||
User: &User{Name: username},
|
User: &User{Name: username},
|
||||||
AccountLink: &AccountLink{Alert: accountAlert},
|
AccountLink: &AccountLink{Alert: accountAlert},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type InputProps struct {
|
|
||||||
Type string `json:",omitempty"`
|
|
||||||
Name string
|
|
||||||
Required bool `json:",omitempty"`
|
|
||||||
Value string
|
|
||||||
Error string `json:",omitempty"`
|
|
||||||
MinLength uint `json:",omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *InputProps) Validate() bool {
|
|
||||||
if p.Required && p.Value == "" {
|
|
||||||
p.Error = fmt.Sprintf("%v is required", p.Name)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
value_len := len(p.Value)
|
|
||||||
if p.MinLength > 0 && int(p.MinLength) > value_len {
|
|
||||||
p.Error = fmt.Sprintf("%v requires at least %v characters (currently %v characters)", p.Name, p.MinLength, value_len)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
var Template *template.Template
|
var Template *template.Template
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user