package middleware import ( "fmt" "net/http" "os" "time" "uptime/backend/database" "uptime/backend/models" "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt/v5" ) func RequireAuth(c *gin.Context) { tokenString, err := c.Cookie("Authorization") if err != nil { c.AbortWithStatus(http.StatusUnauthorized) return } token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) } return []byte(os.Getenv("SECRET")), nil }) if err != nil { c.AbortWithStatus(http.StatusUnauthorized) return } if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid { if float64(time.Now().Unix()) > claims["exp"].(float64) { c.AbortWithStatus(http.StatusUnauthorized) return } var user models.User database.DB.First(&user, claims["sub"]) if user.ID == 0 { c.AbortWithStatus(http.StatusUnauthorized) return } c.Set("user", user) c.Next() } else { c.AbortWithStatus(http.StatusUnauthorized) } }