1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 03:36:16 +00:00

Actually check for illegal username, pretty errors

This commit is contained in:
handlerug 2021-07-01 15:45:03 +07:00
parent 530b2a97e1
commit 7073f9bce0
No known key found for this signature in database
GPG Key ID: 38009F0605051491
3 changed files with 19 additions and 4 deletions

View File

@ -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.

View File

@ -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 {

View File

@ -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(
`<main class="main-width"><p>%s</p><p><a href="/register">Try again<a></p></main>`,
err.Error(),
),
user.FromRequest(rq),
),
)
} else {
user.LoginDataHTTP(w, rq, username, password)
http.Redirect(w, rq, "/"+rq.URL.RawQuery, http.StatusSeeOther)