| | package middleware
|
| |
|
| | import (
|
| | "net/http"
|
| | "opus-api/internal/service"
|
| | "strings"
|
| |
|
| | "github.com/gin-gonic/gin"
|
| | )
|
| |
|
| |
|
| | func AuthMiddleware(authService *service.AuthService) gin.HandlerFunc {
|
| | return func(c *gin.Context) {
|
| | authHeader := c.GetHeader("Authorization")
|
| | if authHeader == "" {
|
| | c.JSON(http.StatusUnauthorized, gin.H{"error": "missing authorization header"})
|
| | c.Abort()
|
| | return
|
| | }
|
| |
|
| |
|
| | parts := strings.SplitN(authHeader, " ", 2)
|
| | if len(parts) != 2 || parts[0] != "Bearer" {
|
| | c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid authorization header format"})
|
| | c.Abort()
|
| | return
|
| | }
|
| |
|
| | token := parts[1]
|
| |
|
| |
|
| | userID, err := authService.ValidateToken(token)
|
| | if err != nil {
|
| | c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid or expired token"})
|
| | c.Abort()
|
| | return
|
| | }
|
| |
|
| |
|
| | c.Set("user_id", userID)
|
| | c.Next()
|
| | }
|
| | }
|
| |
|
| |
|
| | func GetUserID(c *gin.Context) (uint, bool) {
|
| | userID, exists := c.Get("user_id")
|
| | if !exists {
|
| | return 0, false
|
| | }
|
| | id, ok := userID.(uint)
|
| | return id, ok
|
| | } |