feat: add update password
ci-api / test (push) Successful in 24m40s
ci-web / test (push) Canceled after 8m5s

This commit is contained in:
Nuxgrid
2026-07-29 14:18:14 +02:00
parent eda6c0e98a
commit 5a0c1bfe3f
5 changed files with 105 additions and 2 deletions
@@ -16,6 +16,7 @@ func NewGormStore(db *gorm.DB) *GormStore {
type Store interface {
UpdateUsername(id, newUsername string) (auth.User, error)
UpdatePassword(id, passwordHahs string) (auth.User, error)
}
func (s *GormStore) UpdateUsername(id, newUsername string) (auth.User, error) {
@@ -34,3 +35,15 @@ func (s *GormStore) UpdateUsername(id, newUsername string) (auth.User, error) {
return user, nil
}
func (s *GormStore) UpdatePassword(id, passwordHash string) (auth.User, error) {
result := s.db.Model(&auth.User{}).Where("id = ?", id).Update("password", passwordHash)
if result.Error != nil {
return auth.User{}, result.Error
}
var user auth.User
if err := s.db.First(&user, "id = ?", id).Error; err != nil {
return auth.User{}, err
}
return user, nil
}
@@ -4,6 +4,7 @@ import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/omnex/control-plane/api/internal/auth"
)
type Handler struct {
@@ -18,6 +19,10 @@ type newUsername struct {
Username string `json:"username" binding:"required,min=2,max=120"`
}
type newPassword struct {
Password string `json:"password" binding:"required,min=8"`
}
func (h *Handler) UpdateUsernameById(c *gin.Context) {
var u newUsername
@@ -40,3 +45,31 @@ func (h *Handler) UpdateUsernameById(c *gin.Context) {
c.JSON(http.StatusOK, user)
}
func (h *Handler) UpdatePasswordById(c *gin.Context) {
var u newPassword
if err := c.ShouldBindJSON(&u); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
return
}
id, ok := c.MustGet("id").(string)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "id invalide"})
return
}
hash, err := auth.HashPassword(u.Password)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "erreur serveur"})
return
}
user, err := h.store.UpdatePassword(id, hash)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
return
}
c.JSON(http.StatusOK, user)
}