feat(service): lab2 UserService.Create hash password
This commit is contained in:
@@ -23,7 +23,7 @@ func NewUserService(users domain.UserRepository, hasher PasswordHasher) *UserSer
|
||||
return &UserService{users: users, hasher: hasher}
|
||||
}
|
||||
|
||||
func (s *UserService) Create(ctx context.Context, username, displayName string) (*domain.User, error) {
|
||||
func (s *UserService) Create(ctx context.Context, username, displayName, password string) (*domain.User, error) {
|
||||
if !usernamePattern.MatchString(username) {
|
||||
return nil, &domain.ValidationError{
|
||||
Field: "username",
|
||||
@@ -43,11 +43,25 @@ func (s *UserService) Create(ctx context.Context, username, displayName string)
|
||||
}
|
||||
}
|
||||
|
||||
passwordRunes := utf8.RuneCountInString(password)
|
||||
if passwordRunes < 12 || passwordRunes > 128 {
|
||||
return nil, &domain.ValidationError{
|
||||
Field: "password",
|
||||
Message: "длина 12–128 символов",
|
||||
}
|
||||
}
|
||||
|
||||
hash, err := s.hasher.Hash(password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
u := &domain.User{
|
||||
ID: uuid.NewString(),
|
||||
Username: username,
|
||||
DisplayName: displayName,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
ID: uuid.NewString(),
|
||||
Username: username,
|
||||
DisplayName: displayName,
|
||||
PasswordHash: hash,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
if err := s.users.Create(ctx, u); err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user