package auth import ( "net/http" "strings" "repuestera_back/config" "repuestera_back/dto" "repuestera_back/models" "github.com/gin-gonic/gin" "golang.org/x/crypto/bcrypt" ) func Register(c *gin.Context) { var req dto.RegisterRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } email := strings.ToLower(strings.TrimSpace(req.Email)) var existing models.Usuario if err := config.DB.Where("LOWER(email) = ?", email).First(&existing).Error; err == nil { c.JSON(http.StatusConflict, gin.H{"error": "El email ya esta registrado"}) return } hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Error al procesar la contrasena"}) return } user := models.Usuario{ Email: email, Password: string(hashedPassword), Name: req.Name, } if err := config.DB.Create(&user).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusCreated, dto.RegisterResponse{ ID: user.ID, Email: user.Email, Name: user.Name, }) }