Validate password length on the user-update endpoint

PUT /users/update doubles as the password-setup/reset call (userid +
password only) for the new frontend create-password flow, and had no
validation on that field at all.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Suriya
2026-07-21 16:40:47 +05:30
parent 5e998a54eb
commit 9258ce592c

View File

@@ -3,6 +3,7 @@ package controllers
import (
"net/http"
"strconv"
"strings"
"nearle/models"
"nearle/services"
@@ -141,6 +142,17 @@ func (ctl *UserController) UpdateStaff(c *fiber.Ctx) error {
})
}
// This endpoint also doubles as the password-setup/reset call (userid +
// password only, everything else left zero so GORM's Updates skips it) —
// guard the one field that has no validation anywhere else on this path.
if pw := strings.TrimSpace(user.Password); pw != "" && len(pw) < 6 {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"status": false,
"code": http.StatusBadRequest,
"message": "Password must be at least 6 characters",
})
}
if err := ctl.userService.UpdateStaff(user); err != nil {
return c.JSON(fiber.Map{
"status": false,