Improve usability of the user registration
This commit is contained in:
parent
0c2e35bb80
commit
dffd67a9fe
3 changed files with 15 additions and 4 deletions
|
@ -8,10 +8,13 @@ import (
|
|||
"bytes"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"regexp"
|
||||
|
||||
"golang.org/x/crypto/scrypt"
|
||||
)
|
||||
|
||||
var alphaNumeric = regexp.MustCompile(`^[a-zA-Z0-9_\-\.]+$`).MatchString
|
||||
|
||||
type User struct {
|
||||
ID int `sql:"type:serial"`
|
||||
Username string `sql:"type:varchar(255),unique"`
|
||||
|
@ -23,7 +26,7 @@ type User struct {
|
|||
|
||||
func (db *pgDB) AddUser(name string, pass string) error {
|
||||
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()
|
||||
if err != nil {
|
||||
|
@ -118,8 +121,14 @@ func (db *pgDB) getUser(name string) (User, error) {
|
|||
}
|
||||
|
||||
func validUserName(name string) bool {
|
||||
if len(name) < 3 {
|
||||
return false
|
||||
}
|
||||
if !alphaNumeric(name) {
|
||||
return false
|
||||
}
|
||||
switch name {
|
||||
case "", "admin", "webmaster", "postmaster", "info", "root", "news":
|
||||
case "", "admin", "webmaster", "postmaster", "info", "root", "news", "trantor", "librarian", "library", "imperial":
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
|
|
Reference in a new issue