Skip to content

Commit

Permalink
fix length validation bug and refactor a little
Browse files Browse the repository at this point in the history
  • Loading branch information
dreth committed Aug 15, 2024
1 parent eadb0da commit 78761c5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
36 changes: 22 additions & 14 deletions backend/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func Register(c *gin.Context) {
[]int{150, 64, 50, 50, 60, 20},
[]int{5, 1, 1, 1, 1, 1},
[]int{0, 0, 0, 0, 0, 0},
[]bool{false, false, false, false, false, false},
)
// Loop over errors and concatenate the strings to return it all at once
// first check if all errors are nil, if so, nothing to do, otherwise, loop over the errors and concatenate them
Expand Down Expand Up @@ -200,6 +201,7 @@ func Login(c *gin.Context) {
[]int{150, 64},
[]int{5, 1},
[]int{0, 0},
[]bool{false, false},
)
if helper.CheckErrors(lengthErrors) != nil {
errorStr := helper.ConcatenateErrors(lengthErrors)
Expand Down Expand Up @@ -302,8 +304,9 @@ func ModifyUser(c *gin.Context) {
[]string{"NewEmail", "NewPassword", "NewReminderTime", "NewTimezone", "NewTelegramBotAPIKey", "NewTelegramUserID"},
[]string{req.NewEmail, req.NewPassword, req.NewReminderTime, req.NewTimezone, req.NewTelegramBotAPIKey, req.NewTelegramUserID},
[]int{150, 64, 50, 50, 60, 20},
[]int{5, 5, 1, 1, 1, 1},
[]int{0, 0, 1, 1, 1, 1},
[]int{0, 0, 0, 0, 0, 0},
[]bool{true, true, false, false, false, false},
)
if helper.CheckErrors(lengthErrors) != nil {
errorStr := helper.ConcatenateErrors(lengthErrors)
Expand Down Expand Up @@ -391,24 +394,29 @@ func ModifyUser(c *gin.Context) {
return
}

// Update the email in the context
c.Set("Email", req.NewEmail)

// Get the user data post-changes
// Get user data post-changes
userData, err := GetUserData(c)
if helper.HE(c, err, http.StatusInternalServerError, "invalid email or password", true) {
return
}

// Return the new token with the new user data
c.JSON(http.StatusOK, structs.LoginSuccess{
Token: token,
TelegramBotAPIKey: userData.TelegramBotAPIKey,
TelegramUserID: userData.TelegramUserID,
ReminderTime: userData.ReminderTime,
Timezone: userData.Timezone,
Birthdays: userData.Birthdays,
})
// Update the email in the context
if req.NewEmail != "" {
c.Set("Email", req.NewEmail)

// Return the new token with the new user data
c.JSON(http.StatusOK, structs.LoginSuccess{
Token: token,
TelegramBotAPIKey: userData.TelegramBotAPIKey,
TelegramUserID: userData.TelegramUserID,
ReminderTime: userData.ReminderTime,
Timezone: userData.Timezone,
Birthdays: userData.Birthdays,
})
}

// Return the data that calling /me would return
c.JSON(http.StatusOK, userData)
}

// @Summary Delete a user
Expand Down
19 changes: 7 additions & 12 deletions backend/helper/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ func IsValidEmail(email string) bool {
}

// Check the length of a particular string and return an error if it is too long
func CheckStringLength(field string, str string, maxLength int, minLength int, strictLength int) error {
func CheckStringLength(field string, str string, maxLength int, minLength int, strictLength int, allowEmpty bool) error {
if len(str) == 0 {
return StringInappropriateLengthError(field, "empty")
if !allowEmpty {
return StringInappropriateLengthError(field, "empty")
}
} else if len(str) < minLength {
return StringInappropriateLengthError(field, "too short")
} else if len(str) > maxLength {
Expand All @@ -28,7 +30,7 @@ func CheckStringLength(field string, str string, maxLength int, minLength int, s
}

// Check the length of a particular array of string and return an error if it is too long
func CheckArrayStringLength(field []string, str []string, maxLength []int, minLength []int, strictLength []int) []error {
func CheckArrayStringLength(field []string, str []string, maxLength []int, minLength []int, strictLength []int, allowEmpty []bool) []error {
errorsList := []error{}

// Boundary check for array lengths
Expand All @@ -37,15 +39,8 @@ func CheckArrayStringLength(field []string, str []string, maxLength []int, minLe
}

for i := 0; i < len(str); i++ {
if len(str[i]) == 0 {
errorsList = append(errorsList, StringInappropriateLengthError(field[i], "empty"))
} else if len(str[i]) < minLength[i] {
errorsList = append(errorsList, StringInappropriateLengthError(field[i], "too short"))
} else if len(str[i]) > maxLength[i] {
errorsList = append(errorsList, StringInappropriateLengthError(field[i], "too long"))
} else if (strictLength[i] != 0) && (len(str[i]) != strictLength[i]) {
errorsList = append(errorsList, StringInappropriateLengthError(field[i], "incorrect length, should be "+strconv.Itoa(strictLength[i])))
}
err := CheckStringLength(field[i], str[i], maxLength[i], minLength[i], strictLength[i], allowEmpty[i])
errorsList = append(errorsList, err)
}
return errorsList
}

0 comments on commit 78761c5

Please sign in to comment.