Phase 2 completed: Stream Server Auth, Database and Business API

This commit is contained in:
2026-03-16 15:54:41 +08:00
commit c84dd6ea36
16 changed files with 757 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
package api
import (
"net/http"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"hightube/internal/utils"
)
// AuthMiddleware intercepts requests, validates JWT, and injects user_id into context
func AuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
if authHeader == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header is required"})
c.Abort()
return
}
parts := strings.Split(authHeader, " ")
if len(parts) != 2 || parts[0] != "Bearer" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header format must be Bearer {token}"})
c.Abort()
return
}
tokenStr := parts[1]
userIDStr, err := utils.ParseToken(tokenStr)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid or expired token"})
c.Abort()
return
}
userID, _ := strconv.ParseUint(userIDStr, 10, 32)
c.Set("user_id", uint(userID))
c.Next()
}
}