package auth import ( "log" "net/http" "repuestera_back/jwtutils" r "repuestera_back/repositorios" "repuestera_back/services" "github.com/gin-gonic/gin" ) func Refresh(c *gin.Context) { var req struct { RefreshToken string `json:"refresh_token" binding:"required"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } claims, err := jwtutils.ValidarTokenDeTipo(req.RefreshToken, jwtutils.TokenTypeRefresh) if err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": "refresh token inválido"}) return } sub, ok := jwtutils.ClaimUint(claims, jwtutils.ClaimSubject) if !ok { c.JSON(http.StatusUnauthorized, gin.H{"error": "refresh token inválido"}) return } user, err := r.ObtenerRepositorios().Usuario.ObtenerPorID(sub) if err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": "usuario no encontrado"}) return } if !user.Activo { c.JSON(http.StatusForbidden, gin.H{"error": "la cuenta está deshabilitada"}) return } empresaID, _ := jwtutils.ClaimUint(claims, jwtutils.ClaimEmpresaID) respuesta, err := services.ConstruirSesion(user.ID, empresaID) if err != nil { log.Printf("refresh: no se pudo renovar la sesión del usuario %d: %v", user.ID, err) c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, respuesta) }