feat(service): lab2 UserService.Create hash password

This commit is contained in:
Голиков Maximillian Антон
2026-10-01 15:57:55 +03:00
parent cfabbfb5a0
commit df6dcd1d84
3 changed files with 21 additions and 7 deletions
+19 -5
View File
@@ -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