diff --git a/main.go b/main.go index b5c60c5..8ad5de9 100644 --- a/main.go +++ b/main.go @@ -1,4 +1,3 @@ -//go:generate go get -u github.com/valyala/quicktemplate/qtc //go:generate qtc -dir=views //go:generate qtc -dir=tree // Command mycorrhiza is a program that runs a mycorrhiza wiki. diff --git a/user/net.go b/user/net.go index 2748f93..7506f89 100644 --- a/user/net.go +++ b/user/net.go @@ -2,12 +2,13 @@ package user import ( "errors" - "github.com/bouncepaw/mycorrhiza/cfg" + "fmt" "log" "net/http" "strconv" "time" + "github.com/bouncepaw/mycorrhiza/cfg" "github.com/bouncepaw/mycorrhiza/util" "golang.org/x/crypto/bcrypt" ) @@ -49,6 +50,7 @@ func Register(username, password string) error { return errors.New("Username " + username + " is taken already.") case !util.IsPossibleUsername(username): log.Println("Illegal username:", username) + return fmt.Errorf("Illegal username \"%s\".", username) } hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil { diff --git a/web/auth.go b/web/auth.go index 51a0c89..33308d0 100644 --- a/web/auth.go +++ b/web/auth.go @@ -1,11 +1,13 @@ package web import ( - "github.com/bouncepaw/mycorrhiza/cfg" + "fmt" "io" "log" + "mime" "net/http" + "github.com/bouncepaw/mycorrhiza/cfg" "github.com/bouncepaw/mycorrhiza/user" "github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/views" @@ -46,7 +48,19 @@ func handlerRegister(w http.ResponseWriter, rq *http.Request) { err = user.Register(username, password) ) if err != nil { - io.WriteString(w, err.Error()) + w.Header().Set("Content-Type", mime.TypeByExtension(".html")) + w.WriteHeader(http.StatusBadRequest) + fmt.Fprint( + w, + views.BaseHTML( + "Register", + fmt.Sprintf( + `

%s

Try again

`, + err.Error(), + ), + user.FromRequest(rq), + ), + ) } else { user.LoginDataHTTP(w, rq, username, password) http.Redirect(w, rq, "/"+rq.URL.RawQuery, http.StatusSeeOther)