package middleware import ( "net/http" "github.com/yourusername/markedit/internal/auth" "github.com/yourusername/markedit/internal/database" ) // RequireAuth ensures the user is authenticated func RequireAuth(db *database.DB) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { session, err := auth.GetSession(r) if err != nil { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } userID, ok := auth.GetUserID(session) if !ok || userID == 0 { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } // Verify user exists in database _, err = db.GetUserByID(userID) if err != nil { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } next.ServeHTTP(w, r) }) } }