Normalize Username Handling in Customer and User Services

- Updated the Register, Update, and Login methods in both customer and user services to ensure usernames are consistently stored and processed in lowercase.
- Modified notification messages to reflect the lowercase username format, enhancing clarity and consistency in user communications.
- Improved username existence checks during registration and updates to prevent case-sensitive duplicates, ensuring a more robust user experience.
This commit is contained in:
n.nakhostin
2025-09-24 12:29:49 +03:30
parent a342da193e
commit 080c1d18e5
2 changed files with 15 additions and 15 deletions
+6 -6
View File
@@ -65,7 +65,7 @@ func (s *userService) Register(ctx context.Context, form *CreateUserForm) (*User
}
// Check if username already exists
existingUser, _ = s.repository.GetByUsername(ctx, form.Username)
existingUser, _ = s.repository.GetByUsername(ctx, strings.ToLower(form.Username))
if existingUser != nil {
return nil, errors.New("username already exists")
}
@@ -88,7 +88,7 @@ func (s *userService) Register(ctx context.Context, form *CreateUserForm) (*User
// Create user
user := &User{
FullName: form.FullName,
Username: form.Username,
Username: strings.ToLower(form.Username),
Email: form.Email,
Password: string(hashedPassword),
Role: role,
@@ -121,7 +121,7 @@ func (s *userService) Register(ctx context.Context, form *CreateUserForm) (*User
s.notification.SendEmail(ctx, notification.Model{
Recipient: form.Email,
Title: "Welcome to Opplens",
Message: fmt.Sprintf("Welcome to Opplens\nUsername: %s\nPassword: %s", form.Username, form.Password),
Message: fmt.Sprintf("Welcome to Opplens\nUsername: %s\nPassword: %s", strings.ToLower(form.Username), form.Password),
Type: "info",
Priority: "important",
UserID: user.ID.Hex(),
@@ -145,11 +145,11 @@ func (s *userService) Update(ctx context.Context, id string, form *UpdateUserFor
if form.Username != nil {
// Check if username already exists
existingUser, _ := s.repository.GetByUsername(ctx, *form.Username)
existingUser, _ := s.repository.GetByUsername(ctx, strings.ToLower(*form.Username))
if existingUser != nil && existingUser.ID.Hex() != id {
return nil, errors.New("username already exists")
}
user.Username = *form.Username
user.Username = strings.ToLower(*form.Username)
}
if form.Email != nil {
@@ -318,7 +318,7 @@ func (s *userService) Login(ctx context.Context, form *LoginForm) (*AuthResponse
if strings.Contains(form.Username, "@") {
user, err = s.repository.GetByEmail(ctx, form.Username)
} else {
user, err = s.repository.GetByUsername(ctx, form.Username)
user, err = s.repository.GetByUsername(ctx, strings.ToLower(form.Username))
}
if err != nil {