server/controller/UserController.go

50 lines
999 B
Go

package controller
import (
"errors"
"git.tek.govt.hu/dowerx/chat/server/dao"
"git.tek.govt.hu/dowerx/chat/server/model"
"golang.org/x/crypto/bcrypt"
)
type UserController struct {
userDAO dao.IUserDAO
}
const (
MIN_USERNAME_LENGTH int = 3
MIN_PASSWORD_LENGTH int = 6
HASH_COST int = bcrypt.DefaultCost
)
func (c *UserController) init() error {
userDAO, err := dao.MakeUserDAO()
c.userDAO = userDAO
return err
}
func (c UserController) Register(username string, password string, repeatPassword string) error {
if len(username) < MIN_USERNAME_LENGTH {
return errors.New("username too short")
}
if len(password) < MIN_PASSWORD_LENGTH {
return errors.New("password too short")
}
if password != repeatPassword {
return errors.New("passwords don't match")
}
hash, err := bcrypt.GenerateFromPassword([]byte(password), HASH_COST)
if err != nil {
return err
}
return c.userDAO.Create(model.User{
Username: username,
PasswordHash: string(hash),
})
}