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
|
||||
|
|
|
@ -39,13 +39,15 @@ func createUserHandler(h handler) {
|
|||
confirmPass := h.r.FormValue("confirmPass")
|
||||
if pass != confirmPass {
|
||||
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 {
|
||||
user := h.r.FormValue("user")
|
||||
err := h.db.AddUser(user, pass)
|
||||
if err == nil {
|
||||
h.sess.Notify("Account created!", "Welcome "+user+". Now you can login", "success")
|
||||
} 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)
|
||||
|
|
|
@ -82,7 +82,7 @@
|
|||
</ul>
|
||||
</li>
|
||||
{{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}}
|
||||
</ul>
|
||||
</div><!--/.nav-collapse -->
|
||||
|
|
Reference in a new issue