Improve usability of the user registration

This commit is contained in:
Las Zenow 2018-04-09 20:52:40 +00:00
parent 0c2e35bb80
commit dffd67a9fe
3 changed files with 15 additions and 4 deletions

View file

@ -8,10 +8,13 @@ import (
"bytes" "bytes"
"crypto/rand" "crypto/rand"
"errors" "errors"
"regexp"
"golang.org/x/crypto/scrypt" "golang.org/x/crypto/scrypt"
) )
var alphaNumeric = regexp.MustCompile(`^[a-zA-Z0-9_\-\.]+$`).MatchString
type User struct { type User struct {
ID int `sql:"type:serial"` ID int `sql:"type:serial"`
Username string `sql:"type:varchar(255),unique"` Username string `sql:"type:varchar(255),unique"`
@ -23,7 +26,7 @@ type User struct {
func (db *pgDB) AddUser(name string, pass string) error { func (db *pgDB) AddUser(name string, pass string) error {
if !validUserName(name) { if !validUserName(name) {
return errors.New("Invalid user name") return errors.New("Invalid user name. Username needs to have at least 3 characters and can only be letters, numbers, '-', '_' and '.'.")
} }
num, err := db.sql.Model(&User{}).Where("lower(username) = lower(?)", name).Count() num, err := db.sql.Model(&User{}).Where("lower(username) = lower(?)", name).Count()
if err != nil { if err != nil {
@ -118,8 +121,14 @@ func (db *pgDB) getUser(name string) (User, error) {
} }
func validUserName(name string) bool { func validUserName(name string) bool {
if len(name) < 3 {
return false
}
if !alphaNumeric(name) {
return false
}
switch name { switch name {
case "", "admin", "webmaster", "postmaster", "info", "root", "news": case "", "admin", "webmaster", "postmaster", "info", "root", "news", "trantor", "librarian", "library", "imperial":
return false return false
default: default:
return true return true

View file

@ -39,13 +39,15 @@ func createUserHandler(h handler) {
confirmPass := h.r.FormValue("confirmPass") confirmPass := h.r.FormValue("confirmPass")
if pass != confirmPass { if pass != confirmPass {
h.sess.Notify("Registration error!", "Passwords don't match", "error") h.sess.Notify("Registration error!", "Passwords don't match", "error")
} else if pass == "" {
h.sess.Notify("Registration error!", "The password can't be empty", "error")
} else { } else {
user := h.r.FormValue("user") user := h.r.FormValue("user")
err := h.db.AddUser(user, pass) err := h.db.AddUser(user, pass)
if err == nil { if err == nil {
h.sess.Notify("Account created!", "Welcome "+user+". Now you can login", "success") h.sess.Notify("Account created!", "Welcome "+user+". Now you can login", "success")
} else { } else {
h.sess.Notify("Registration error!", "There was some database problem, if it keeps happening please inform me", "error") h.sess.Notify("Registration error!", err.Error(), "error")
} }
} }
h.sess.Save(h.w, h.r) h.sess.Save(h.w, h.r)

View file

@ -82,7 +82,7 @@
</ul> </ul>
</li> </li>
{{else}} {{else}}
<li><a data-toggle="modal" href="/login/#login"><i class="icon-user icon-white"></i></a></li> <li><a data-toggle="modal" href="/login/#login"><i class="icon-user icon-white"></i> <small>Login/SignUp</small></a></li>
{{end}} {{end}}
</ul> </ul>
</div><!--/.nav-collapse --> </div><!--/.nav-collapse -->