Private
Public Access
fix+feat: añadido crear usuario y separado en componentes los modals
This commit is contained in:
1 parent
eff116fab0
commit
62ec25b768
14 files changed
+387
-131
No files matched your search
@@ -0,0 +1,71 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"repuestera_back/config"
|
||||
"repuestera_back/dto"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
"repuestera_back/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func CambiarContraseña(c *gin.Context) {
|
||||
var req dto.CambiarContraseñaRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if len(req.NewPassword) < 8 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "La nueva contraseña debe tener mas de 8 caracteres"})
|
||||
return
|
||||
}
|
||||
|
||||
authUserID, _ := c.Get("userID")
|
||||
authUserIDUint, _ := authUserID.(uint)
|
||||
|
||||
targetID := authUserIDUint
|
||||
if req.UserID != 0 {
|
||||
targetID = req.UserID
|
||||
}
|
||||
|
||||
esAdmin := utils.UsuarioTienePermiso(c, 3)
|
||||
|
||||
if targetID != authUserIDUint && !esAdmin {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "No tienes permiso para cambiar la contraseña de otro usuario"})
|
||||
return
|
||||
}
|
||||
|
||||
var user models_seguridad.Usuario
|
||||
if err := config.DB.First(&user, targetID).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "usuario no encontrado"})
|
||||
return
|
||||
}
|
||||
|
||||
if !esAdmin {
|
||||
if req.OldPassword == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Se requiere la contraseña actual"})
|
||||
return
|
||||
}
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(req.OldPassword)); err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "contraseña actual incorrecta"})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.NewPassword), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error al procesar la contraseña"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := config.DB.Model(&user).Update("password", string(hashedPassword)).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error al actualizar la contraseña"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Contraseña actualizada exitosamente"})
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"repuestera_back/config"
|
||||
"repuestera_back/dto"
|
||||
"repuestera_back/jwtutils"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -14,8 +15,6 @@ import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
var JWTSecret = []byte("supersecretkey123456789")
|
||||
|
||||
func Login(c *gin.Context) {
|
||||
var req dto.LoginRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
@@ -65,5 +64,5 @@ func generarToken(user models_seguridad.Usuario, duration time.Duration) (string
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
return token.SignedString(JWTSecret)
|
||||
return token.SignedString(jwtutils.JWTSecret)
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"repuestera_back/config"
|
||||
"repuestera_back/dto"
|
||||
"repuestera_back/jwtutils"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -31,7 +32,7 @@ func Me(c *gin.Context) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, jwt.ErrSignatureInvalid
|
||||
}
|
||||
return JWTSecret, nil
|
||||
return jwtutils.JWTSecret, nil
|
||||
})
|
||||
|
||||
if err != nil || !token.Valid {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"repuestera_back/config"
|
||||
"repuestera_back/dto"
|
||||
"repuestera_back/jwtutils"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -24,7 +25,7 @@ func Refresh(c *gin.Context) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, jwt.ErrSignatureInvalid
|
||||
}
|
||||
return JWTSecret, nil
|
||||
return jwtutils.JWTSecret, nil
|
||||
})
|
||||
|
||||
if err != nil || !token.Valid {
|
||||
|
||||
Reference in new issue
Block a user