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:
@@ -114,7 +114,7 @@ func (s *customerService) Register(ctx context.Context, form *CreateCustomerForm
|
||||
Status: CustomerStatusActive,
|
||||
Companies: Companies,
|
||||
FullName: form.FullName,
|
||||
Username: form.Username,
|
||||
Username: strings.ToLower(form.Username),
|
||||
Email: form.Email,
|
||||
Password: string(hashedPassword),
|
||||
Phone: form.Phone,
|
||||
@@ -148,7 +148,7 @@ func (s *customerService) Register(ctx context.Context, form *CreateCustomerForm
|
||||
s.notification.SendEmail(ctx, notification.Model{
|
||||
Recipient: customer.Email,
|
||||
Title: "Welcome to Opplens",
|
||||
Message: fmt.Sprintf("Welcome to Opplens\nUsername: %s\nPassword: %s", customer.Username, form.Password),
|
||||
Message: fmt.Sprintf("Welcome to Opplens\nUsername: %s\nPassword: %s", strings.ToLower(customer.Username), form.Password),
|
||||
Type: "info",
|
||||
Priority: "important",
|
||||
UserID: customer.ID.Hex(),
|
||||
@@ -308,12 +308,12 @@ func (s *customerService) Update(ctx context.Context, id string, form *UpdateCus
|
||||
}
|
||||
|
||||
// Check if username already exists (if changing username)
|
||||
if form.Username != "" && form.Username != customer.Username {
|
||||
existingCustomer, _ := s.repository.GetByUsername(ctx, form.Username)
|
||||
if form.Username != "" && strings.ToLower(form.Username) != customer.Username {
|
||||
existingCustomer, _ := s.repository.GetByUsername(ctx, strings.ToLower(form.Username))
|
||||
if existingCustomer != nil {
|
||||
return nil, errors.New("customer with this username already exists")
|
||||
}
|
||||
customer.Username = form.Username
|
||||
customer.Username = strings.ToLower(form.Username)
|
||||
}
|
||||
|
||||
// Handle company assignments
|
||||
@@ -568,14 +568,14 @@ func (s *customerService) Activate(ctx context.Context, id string) error {
|
||||
// Login handles customer authentication
|
||||
func (s *customerService) Login(ctx context.Context, form *LoginForm) (*AuthResponse, error) {
|
||||
s.logger.Info("Customer login attempt", map[string]interface{}{
|
||||
"username": form.Username,
|
||||
"username": strings.ToLower(form.Username),
|
||||
})
|
||||
|
||||
// Get customer by username
|
||||
customer, err := s.repository.GetByUsername(ctx, form.Username)
|
||||
customer, err := s.repository.GetByUsername(ctx, strings.ToLower(form.Username))
|
||||
if err != nil {
|
||||
s.logger.Warn("Customer login failed - username not found", map[string]interface{}{
|
||||
"username": form.Username,
|
||||
"username": strings.ToLower(form.Username),
|
||||
})
|
||||
return nil, errors.New("invalid credentials")
|
||||
}
|
||||
@@ -583,7 +583,7 @@ func (s *customerService) Login(ctx context.Context, form *LoginForm) (*AuthResp
|
||||
// Check if customer is active
|
||||
if customer.Status != CustomerStatusActive {
|
||||
s.logger.Warn("Customer login failed - account not active", map[string]interface{}{
|
||||
"username": form.Username,
|
||||
"username": strings.ToLower(form.Username),
|
||||
"customer_id": customer.ID,
|
||||
"status": string(customer.Status),
|
||||
})
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user