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,
|
Status: CustomerStatusActive,
|
||||||
Companies: Companies,
|
Companies: Companies,
|
||||||
FullName: form.FullName,
|
FullName: form.FullName,
|
||||||
Username: form.Username,
|
Username: strings.ToLower(form.Username),
|
||||||
Email: form.Email,
|
Email: form.Email,
|
||||||
Password: string(hashedPassword),
|
Password: string(hashedPassword),
|
||||||
Phone: form.Phone,
|
Phone: form.Phone,
|
||||||
@@ -148,7 +148,7 @@ func (s *customerService) Register(ctx context.Context, form *CreateCustomerForm
|
|||||||
s.notification.SendEmail(ctx, notification.Model{
|
s.notification.SendEmail(ctx, notification.Model{
|
||||||
Recipient: customer.Email,
|
Recipient: customer.Email,
|
||||||
Title: "Welcome to Opplens",
|
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",
|
Type: "info",
|
||||||
Priority: "important",
|
Priority: "important",
|
||||||
UserID: customer.ID.Hex(),
|
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)
|
// Check if username already exists (if changing username)
|
||||||
if form.Username != "" && form.Username != customer.Username {
|
if form.Username != "" && strings.ToLower(form.Username) != customer.Username {
|
||||||
existingCustomer, _ := s.repository.GetByUsername(ctx, form.Username)
|
existingCustomer, _ := s.repository.GetByUsername(ctx, strings.ToLower(form.Username))
|
||||||
if existingCustomer != nil {
|
if existingCustomer != nil {
|
||||||
return nil, errors.New("customer with this username already exists")
|
return nil, errors.New("customer with this username already exists")
|
||||||
}
|
}
|
||||||
customer.Username = form.Username
|
customer.Username = strings.ToLower(form.Username)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle company assignments
|
// Handle company assignments
|
||||||
@@ -568,14 +568,14 @@ func (s *customerService) Activate(ctx context.Context, id string) error {
|
|||||||
// Login handles customer authentication
|
// Login handles customer authentication
|
||||||
func (s *customerService) Login(ctx context.Context, form *LoginForm) (*AuthResponse, error) {
|
func (s *customerService) Login(ctx context.Context, form *LoginForm) (*AuthResponse, error) {
|
||||||
s.logger.Info("Customer login attempt", map[string]interface{}{
|
s.logger.Info("Customer login attempt", map[string]interface{}{
|
||||||
"username": form.Username,
|
"username": strings.ToLower(form.Username),
|
||||||
})
|
})
|
||||||
|
|
||||||
// Get customer by 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 {
|
if err != nil {
|
||||||
s.logger.Warn("Customer login failed - username not found", map[string]interface{}{
|
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")
|
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
|
// Check if customer is active
|
||||||
if customer.Status != CustomerStatusActive {
|
if customer.Status != CustomerStatusActive {
|
||||||
s.logger.Warn("Customer login failed - account not active", map[string]interface{}{
|
s.logger.Warn("Customer login failed - account not active", map[string]interface{}{
|
||||||
"username": form.Username,
|
"username": strings.ToLower(form.Username),
|
||||||
"customer_id": customer.ID,
|
"customer_id": customer.ID,
|
||||||
"status": string(customer.Status),
|
"status": string(customer.Status),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ func (s *userService) Register(ctx context.Context, form *CreateUserForm) (*User
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if username already exists
|
// Check if username already exists
|
||||||
existingUser, _ = s.repository.GetByUsername(ctx, form.Username)
|
existingUser, _ = s.repository.GetByUsername(ctx, strings.ToLower(form.Username))
|
||||||
if existingUser != nil {
|
if existingUser != nil {
|
||||||
return nil, errors.New("username already exists")
|
return nil, errors.New("username already exists")
|
||||||
}
|
}
|
||||||
@@ -88,7 +88,7 @@ func (s *userService) Register(ctx context.Context, form *CreateUserForm) (*User
|
|||||||
// Create user
|
// Create user
|
||||||
user := &User{
|
user := &User{
|
||||||
FullName: form.FullName,
|
FullName: form.FullName,
|
||||||
Username: form.Username,
|
Username: strings.ToLower(form.Username),
|
||||||
Email: form.Email,
|
Email: form.Email,
|
||||||
Password: string(hashedPassword),
|
Password: string(hashedPassword),
|
||||||
Role: role,
|
Role: role,
|
||||||
@@ -121,7 +121,7 @@ func (s *userService) Register(ctx context.Context, form *CreateUserForm) (*User
|
|||||||
s.notification.SendEmail(ctx, notification.Model{
|
s.notification.SendEmail(ctx, notification.Model{
|
||||||
Recipient: form.Email,
|
Recipient: form.Email,
|
||||||
Title: "Welcome to Opplens",
|
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",
|
Type: "info",
|
||||||
Priority: "important",
|
Priority: "important",
|
||||||
UserID: user.ID.Hex(),
|
UserID: user.ID.Hex(),
|
||||||
@@ -145,11 +145,11 @@ func (s *userService) Update(ctx context.Context, id string, form *UpdateUserFor
|
|||||||
|
|
||||||
if form.Username != nil {
|
if form.Username != nil {
|
||||||
// Check if username already exists
|
// 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 {
|
if existingUser != nil && existingUser.ID.Hex() != id {
|
||||||
return nil, errors.New("username already exists")
|
return nil, errors.New("username already exists")
|
||||||
}
|
}
|
||||||
user.Username = *form.Username
|
user.Username = strings.ToLower(*form.Username)
|
||||||
}
|
}
|
||||||
|
|
||||||
if form.Email != nil {
|
if form.Email != nil {
|
||||||
@@ -318,7 +318,7 @@ func (s *userService) Login(ctx context.Context, form *LoginForm) (*AuthResponse
|
|||||||
if strings.Contains(form.Username, "@") {
|
if strings.Contains(form.Username, "@") {
|
||||||
user, err = s.repository.GetByEmail(ctx, form.Username)
|
user, err = s.repository.GetByEmail(ctx, form.Username)
|
||||||
} else {
|
} else {
|
||||||
user, err = s.repository.GetByUsername(ctx, form.Username)
|
user, err = s.repository.GetByUsername(ctx, strings.ToLower(form.Username))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user